Access file that is being used by another application using FileStream

public void WriteToBinaryFile(string filePath, ListView listview)
{
FileStream fsWrite = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite); // I GET AN EXCEPTION HERE
BinaryFormatter binfmt = new BinaryFormatter();
InvokeIfRequired(listview,()=> { binfmt.Serialize(fsWrite, new ArrayList(listview.Items)); });

fsWrite.Close();
}
public void WriteToBinaryFile(string filePath, ListView listview)
{
FileStream fsWrite = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite); // I GET AN EXCEPTION HERE
BinaryFormatter binfmt = new BinaryFormatter();
InvokeIfRequired(listview,()=> { binfmt.Serialize(fsWrite, new ArrayList(listview.Items)); });

fsWrite.Close();
}
8 Replies
Connor
Connor2y ago
You need to change the FileShare property. using (FileStream stream = new FileStream("file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // Read from or write to the file here } Sorry mobile Do that in the first application, then the second one will be able to access it
Diesel Geezer
Diesel GeezerOP2y ago
there is no second or first application. I dont have access to the source code of whatever application is accessing that file to change that lol.
Connor
Connor2y ago
Hmm
Diesel Geezer
Diesel GeezerOP2y ago
all i have is my application which is trying to write to a text file thats all
mindhardt
mindhardt2y ago
Please do not use ArrayList, it is rudiment from the pre-generic era, stick to List<T>
Connor
Connor2y ago
I think your only option is to wait for the first application to quit using the file I don’t think there’s any work around
mindhardt
mindhardt2y ago
Both apps need to specify FileShare, otherwise OS will not allow accessing file concurrently
Diesel Geezer
Diesel GeezerOP2y ago
oh alright that makes sense thanks
Want results from more Discord servers?
Add your server