views:

643

answers:

6

I am trying to write some text to the file using StreamWriter and getting the path for the file from FolderDialog selected folder. My code works fine if the file does not already exist. but if the file already exist it throws the Exception that the file is in used by other process.

using(StreamWriter sw = new StreamWriter(FolderDialog.SelectedPath + @"\my_file.txt")
{
    sw.writeLine("blablabla");
}

Now if I write like this:

using(StreamWriter sw = new StreamWriter(@"C:\some_folder\my_file.txt")

it works fine with an existing file.

A: 

This is a cheap answer, but have you tried this workaround?

string sFileName= FolderDialog.SelectedPath + @"\my_file.txt";
using(StreamWriter sw = new StreamWriter(sFileName))
{
  sw.writeLine("blablabla");
}

The other thing I would suggest is verifying that FolderDialog.SelectedPath + "\my_file.txt" is equal to the hard coded path of "C:\some_folder\my_file.txt".

Mayo
A: 

Check whether the file is in fact in use by some other process.

To do that, run Process Explorer, press Ctrl+F, type the filename, and click Find.

As an aside, the best way to accomplish this task is like this:

using(StreamWriter sw = File.AppendText(Path.Combine(FolderDialog.SelectedPath, @"my_file.txt")))

EDIT: Do NOT put a slash in the second argument to Path.Combine.

SLaks
A: 

The file is already in use, so it cannot be overwritten. However, note that this message isn't always entirely accurate - the file may in fact be in use by your own process. Check your usage patterns.

Abraham Pinzur
+2  A: 

It may have to do with the way you are combining your path and filename. Give this a try:

using(StreamWriter sw = new StreamWriter(
    Path.Combine(FolderDialog.SelectedPath, "my_file.txt"))
{
    sw.writeLine("blablabla");
}

Also, check to make sure the FolderDialog.SelectedPath value isn't blank. :)

Jon Tackabury
A: 

@Mayo i had already checked this thing.

@Jon Tackabury well this is also not working, and yes i have checked that dialog has path, its not empty

*

@Slaks this method works a little bit as now no already in use exception comes but its now not writing the file, the file remains empty.

*

A@braham Pinzur yep that's 100% checked that file is no where in use.

It's an exercise in debugging. My suggestion is to pass a variable to the StreamWriter constructor. Initialize the variable with a static path - this should work. Then modify the variable with FolderDialog.SelectedPath. If the second attempt doesn't work - the resulting paths *must* be different - use the debugger to see exactly how they are different.
Mayo
if i give static path to variable and pass the variable to StreamWriter it works without any problem but when i replace the static path with Folder.SelectedPath it gives exception
See my edit. by putting a slash in the second part of the path, my code was actually writing to `C:\my_file.txt`. Try it without the slash.
SLaks
Use the debugger (or some output mechanism) and compare the value of the variable in each case. There has to be a difference - it'll be something simple like an extra character.
Mayo
A: 

well i check the sample code in a test project and it works, this shows that i have some mistake in my method, i should revise my method.

by the way i am working in Background Worker's doWork method. can it be a problem ???