I am writting a small app to export and import data from database using .NET DataSets and XML and as part of that I am doing the following.
StreamWriter sw = new StreamWriter(file);
sw.Write(xml.OuterXml);
sw.Close();
The problem is that the close method closes the FileStream (file parameter passed to constructor) but doesn't release the file lock. The file is one that the program I have written creates so I know nothing else is locking it.
Is there something I am doing wrong or is this a windows bug?
EDIT
Yes 'file' is a FileStream object and I naively assumed that calling close() on the stream that wraps the files stream would also cleanup and dispose the underlying file stream by calling the FileStream.close method. But i'm not sure about that any more.
Wrapping this in a using block still has the same effect.
One extra note is that the filestream object is created in a different method but that shouldn't make any difference
Try using this instead:
using (StreamWriter sw = new StreamWriter(file))
sw.Write(xml.OuterXml);
(or try to call sw.Dispose() manually)
Would it help to wrap the file stream in a using or simply use the overloaded method for creating the stream writer:
using (FileStream fs = new FileStream("path", FileMode.Append))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(xml.OuterXml);
sw.Close();
}
or:
using (StreamWriter sw = new StreamWriter("path"))
{
sw.Write(xml.OuterXml);
sw.Close();
}
I suspect the problem is in the code creating or using the FileStream. Perhaps you could elaborate on how you use the FileStream object you create.
Related
Maybe it is a trival question, but it's bothering me. And don't shout laud if it is a duplicate - I tried to search, but there are so many questions regarding using that it was hard for me to find the answer.
I've a code like this:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
writeFile.WriteLine("Example");
And my questions are: What happens to my created IsolatedStorageFileStream, when StreamWriter is disposed, while leaving using? Will it be also disposed?
Is there any difference in comparison to this code:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = ISF.CreateFile("example.txt"))
using (StreamWriter writeFile = new StreamWriter(stream))
writeFile.WriteLine("Example");
Thanks in advance.
You have a constructor for StreamWriter (NET Framework 4.5 only) that allows specifying the leaveOpen boolean that defines whether your instance takes ownership of the underlying stream or not.
If not specified (as in your example, or for previous versions of the framework), by default it's false, so closing (or disposing) the instance closes the underlying stream.
Unless you set the leaveOpen parameter to true, the StreamWriter
object calls Dispose() on the provided Stream object when
StreamWriter.Dispose is called.
So there is no difference between both pieces of code you provided.
Once it leaves the using block, Dispose is called.
using Statement (C# Reference)
The using statement calls the Dispose method on the object in the
correct way, and (when you use it as shown earlier) it also causes the
object itself to go out of scope as soon as Dispose is called. Within
the using block, the object is read-only and cannot be modified or
reassigned.
The using statement ensures that Dispose is called even if an
exception occurs while you are calling methods on the object.
Your stream remains open even though the stream writer is disposed of. You could, for example, open another stream writer and continue writing to the stream.
Use {} always! It makes the intention of your code a lot better.
Your code looks like:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
{
writeFile.WriteLine("Example");
}
}
You can then see that the StreamWriter is executed in the context of the ISF. If I understand the ISF correctly, the ISF should not be closed when the Streamwriter closes the file. And you could open another File in the ISF Block.
I'm using System.IO.File.Create to create a file. I'm not writing to it with a stream writer, just creating it.
I get a server error in the front end when the application tries to open the newly created file - that the file is in use. Garbage collection then seems to come along and a few minutes later all is OK.
Now I know if I was using Streamwriter I would have to close it. Does the same apply to creating?
I've read that opening a stream writer to the file then immediately closing it will fix this, but it seems messy. Is there a simpler way?
Try this:
System.IO.File.Create(FullFName).Close();
File.Create returns a FileStream. You should use it like this:
using (FileStream fs = File.Create(path))
{
//you can use the filstream here to put stuff in the file if you want to
}
Creating the file opens a FileStream to it, hence, locking it (File.Create returns the FileStream).
You must close this stream in order to access the file. This is best done with a using statement:
using(FileStream fs = File.Create(path))
{
}
When using File.Create you get a FileStream returned. Until you either close the stream or until the FileStream object is disposed (by the garbage collector's finaliser) it will remain open and locked.
FileStream implements IDisposable so you can do the following:
using(FileStream fs = File.Create(filename))
{
// Do stuff like write to the file
}
The using statement is "syntactic sugar" and causes the compiler to generate code that is functionally equivalent to:
FileStream fs = File.Create(filename)
try
{
// Do stuff like write to the file
}
finally
{
fs.Dispose();
}
The Dispose method calls Close internally.
I was using:
System.IO.File.Create(sFullFileName);
The above .Create method was not closing the file
I now use:
System.IO.File.WriteAllText(sFullFileName, "Uploading");
This method creates and closes the file (note: I put a string "Uploading" in the file, but i'm sure string.Empty will also work.
The Create method will return a file handle. The file handle should be closed before re-using the file. Please see details in the MSDN article File.Create Method (String).
Summary:
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.
I'm writing code that check files path calculate hash (SHA1) and copy them.
I made sure that I do not lock them like for example using
public static string SHA1(string filePath)
{
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var formatted = string.Empty;
using (var sha1 = new SHA1Managed())
{
byte[] hash = sha1.ComputeHash(fs);
foreach (byte b in hash)
{
formatted += b.ToString("X2");
}
}
return formatted;
}
So how I can, in Visual Studio, find where it does lock the file?
Can you keep the above syntax as and give a try?
using(var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
//Your code goes here.
}
There is a little windows soft : process explorer and in this you can find which process has an handle on a file :
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
Locking usually happens whenever you create a file stream on a file without later closing that stream. Unless you call fs.Close(); in your code, your application will keep the file open (and thus locked).
You could wrap this in a try-finally block or try the code that Siva Gopal posted.
You assumption that opening the file stream with just FileAccess.Read will not lock the file is faulty; the file is locked while it has been opened for a file operation and has not been closed.
A FileStream does not close an opened file until the FileStream is garbage collected or you explicitly call its Close or Dispose method. Either insert such an explicit call as soon as you are done with the file you opened, Or wrap the use of the FileStream in a using statement, which implies the call to Dispose, like other answers suggest.
How do I read and write on a text file without getting the exception that "File is already in use by another app"??
I tried File.readalltext() and File.Appendalltext() functions..I'm just starting out with filestream.
Which would work out best in my scenario? I would appreciate some code snipplets too ..
Thanks
This is all to do with the lock and sharing semantics that you request when opening the file.
Instead of using the shortcut approach of File.ReadAllText(), try looking into using a System.IO.FileStream and a System.IO.StreamReader / System.IO.StreamWriter.
To open a file:
using (var fileStream = new FileStream(#"c:\myFile", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var streamReader = new StreamReader(fileStream))
{
var someText = streamReader.ReadToEnd();
}
Note the FileShare.ReadWrite - this is telling the stream to allow sharing to either other readers or other writers.
For writing try something like
using (var fileStream = new FileStream(#"c:\myFile", FileMode.Create, FileAccess.Write, FileShare.Read))
using (var streamWriter = new StreamWriter(fileStream))
{
streamWriter.WriteLine("some text");
}
Note the FileShare.Read - this is telling the stream to allow sharing to readers only.
Have a read around the System.IO.FileStream and its constructor overloads and you can tailor exactly how it behaves to suit your purpose.
You need to make sure the file is not being used by any other application.
With your own application, you cannot read from a file multiple times without closing the stream between reads.
You need to find out why the file is in use - a tool like FileMon can help finding out.
I've been using File.ReadAllText() to open a CSV file, but every time I forget to close the file in Excel, the application throws an exception because it can't get access to the file.
(Seems crazy to me, I mean the READ in ReadAllText seems pretty clear)
I know that there is File.Open with all the bells and whistles, but is there an 'intermediate' method which doesn't involve messing around with buffers and char arrays?
I think you just want the following:
using (var fileStream = new FileStream("foo.bar", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var textReader = new StreamReader(fileStream))
{
var content = textReader.ReadToEnd();
}
The FileAccess.Read parameter is what is important, to indicate that you only want to read the file. Of course, even to do this, the file must have been opened by Excel in read-share mode (see the FileShare enum in .NET). I haven't tested, so I can't guarantee that Excel does this, though I would expect it does.
[edit]
Here's a method version:
static string ReadAllText(string file)
{
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var textReader = new StreamReader(fileStream))
return textReader.ReadToEnd();
}
If you want to specify file sharing flags in order to open a file that's in use, you're stuck with File.Open().