Multiple Processes accessing the same file causes exception - c#

I wrote a Excel macro which writes excel data to a text file in every 3 seconds.
In the same time, I've a program written in C# language that accesses the same text file, produced by excel macro, and do some process then output to an xml file every 5 seconds.
The Problem I met is that, it sometimes causes an IoException, when the other process is accessing the same txt file. The problem didn't occur frequently. Now I use a try...catch statement in the C# program and On Error statement in the excel macro to catch the exception to avoid stopping my program.
Is there another way to solve this problem?
Exception:
System.IO.IOException: file used by another process

I think some another thread has already acquired the resource (file) that you are now trying to write, which results in IOException.
So in such scenario I would suggest that you should go with the lock synchronization while accessing the file.
Here is very good example that may help you "how to get file release and lock it for use?" - Link
Use this mechanism to lock file and wait for file to be release while attempting to write to file.
Update
Just a little more about referenced link.
It just iterate over while loop until it gets file open to write and then call the callback (Action) passing file instance.

Your problem is that your Excel Macro and your C# program might write to the same file at the same time.
You have two options here, either find a way to synchronize file writing between Excel and your application. Or you can use a Shadow Copy writer in your c# application (not sure if this can be achieved inside excel but one application using it should suffice provided that it always use VSS to write to the file)
You can check AlphaVSS as a VSS handler for your application

Related

C#: How to read a file that's in use by another process

There is one text file in hard drive which continuously writing by some application XYZ.
Now, from application ABC, I am trying to read the file, but I am getting error,
the process cannot access the file because it is being used by another process
Note - I don't have control over XYZ application, still is there any way to read the file from application ABC?
byte[] data = File.ReadAllBytes(File.FullName)
Access the file in read mode so that you will not get this error.
Try this solution
How do I open an already opened file with a .net StreamReader?

Why do some applications lock files and others don't?

In my datalogging application I write to a temporary file of the form ... AppData\Local\Temp\euaxgd5z.csv
This opens by default in Excel.
Process.Start(TempFileLocationBox.Text.ToString());
The next time the timer tries to write more data to this file, an exception is thrown. However if I open it with TextPad
Process.Start("textpad.exe", TempFileLocationBox.Text.ToString());
it can write to the file quite happily and TextPad will ask if I want to reload it. How can I get Excel to behave as nicely as TextPad?
Personally, I wouldn't even try to 'communicate' to excel the fact that you want the file reloaded. I would just write a new file and open a new copy of excel, after closing the old copy if possible.
That way you won't need to worry if the user is using open office instead, for example.

Avoid errors while copying a file and another process writes to it

i am writing a simple folder synchronization app.
Therefore i want at the time a file is created in the source folder it should be
copied to the destination.
My question is : how can i avoid errors when trying to read the source file, since another process might want to write to it ? Should i read the file in Read Write Share mode or is a simple System.IO.File.Copy enough ?
Thanks !
File Copy will lock the file while it is being copied.
If it wasn't locked, the file could be corrupted during the copy by another process
So, when you start the copy process there are two possibilities:
None has a lock on your source file, then File.Copy start its work
and locks the source file.
Someone has a lock on your source file, then File.Copy will throw an
exception.

Need to open file I log to but get file locked exception

hI,
I'm using LOG4NET to log to a txt file. However, one of my forms needs to open the txt file (using streamreader) to read the contents and display them in a .txt file. However, I keep getting an exception stating that the file is locked by another process.
Is there a way around this in LOG4NET?
Thanks
This may be help you.
http://www.ericbt.com/Blog/48
If you open the log file with ReadWrite sharing enabled (FileShare.ReadWrite) it will probably work.
You can always uses Process Monitor from http://www.sysinternals.com to see what is happening.

Write to file that is open in Excel

Suppose I have a program running that periodically adds information to a .CSV file. Is there a way to write to the file while it is already open in Excel? Obviously the changes wouldn't be noticed until the file was re-opened in Excel, but as it stands right now, I'm catching IOException and just starting a new .csv file if the current one is already open.
Excel seems to open the file in exclusive mode, so the only way I can think of would be to write your changes to a temporary file, and then use FileSystemWatcher to see when the file is closed, and overwrite the file.
Not a very good idea, as you could lose data. The whole reason excel locks the file is so that you don't accidentally overwrite changes made in excel.
It sounds like the file is locked. I doubt you will be able to write to that file if it is open in another process.
As a former (and sort of current) VB Programmer, I can tell you Jared is correct - there is no way to do this directly. You can try to copy the file first, make your edits, then attempt to save the file back to its original location until the locked file becomes free. You should be able to copy that file, even while locked.
What about using Excel's object model and automating the addition of the data into the open spreadsheet? You'd probably need to prompt the user somehow to let them know what was happening.

Categories