File sharing query
-
Hi, I'm writing a program that attempts to open a file stream using FileInfo.OpenRead(), this seems to fail when another application is accessing the same file. As I need to be able to read this file whilst another has it open or access it, is there anyway I can get it to open instead of throwing IOException in C#? Thanks. Here's a code snippet //check file exists System.IO.FileInfo info = new System.IO.FileInfo(sFileName); if(info!=null&&info.Exists==true) { //ok, file exits, lets read it in... try { System.IO.Stream stream = info.OpenRead(); //do stuff here stream.Close(); } catch(IOException exception) { }
-
Hi, I'm writing a program that attempts to open a file stream using FileInfo.OpenRead(), this seems to fail when another application is accessing the same file. As I need to be able to read this file whilst another has it open or access it, is there anyway I can get it to open instead of throwing IOException in C#? Thanks. Here's a code snippet //check file exists System.IO.FileInfo info = new System.IO.FileInfo(sFileName); if(info!=null&&info.Exists==true) { //ok, file exits, lets read it in... try { System.IO.Stream stream = info.OpenRead(); //do stuff here stream.Close(); } catch(IOException exception) { }
If the other app has the file opened DenyShareRead at the least, you're screwed. The other problem is that you have to request that the file be opened with Shared Read/Write access. If the other open opens and closes the file frequently, you have to open the file with Shared access so the other app won't fail when it tries to open the file. The FileOpen method does not do this. Instead, you'll have to use the FileStream class to open the file:
FileStream myFileStream = New FileStream("C:\myFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Now, the rub is that you can request Shared access to the file, but it only takes effect for subsequent requests by other processes (apps) to open the file. It will NOT give you access to a file that is opened and locked by another process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
If the other app has the file opened DenyShareRead at the least, you're screwed. The other problem is that you have to request that the file be opened with Shared Read/Write access. If the other open opens and closes the file frequently, you have to open the file with Shared access so the other app won't fail when it tries to open the file. The FileOpen method does not do this. Instead, you'll have to use the FileStream class to open the file:
FileStream myFileStream = New FileStream("C:\myFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Now, the rub is that you can request Shared access to the file, but it only takes effect for subsequent requests by other processes (apps) to open the file. It will NOT give you access to a file that is opened and locked by another process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
:-D Hey Dave, thanks, that worked a treat! Good stuff.