Qn: Getting Around Windows File Lockup Property Using C# -- using FileStream & Stream Writer

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am encountering a problem in developing a software for my business. The task I am trying to achieve is that, I need to update a file (.txt) while that file is being constantly accessed by another program.<br/>
<br/>
<br/>
The ultimate setting is this: I will have a software running, constantly reading the content of a feed file (call it info.txt). I have another script running, constantly updating the feed file (info.txt).<br/>
<br/>
<br/>
I realized the serious conflict in using the file after I implemented the above setting. With the software (call it AAA) running, I cant make an edit to info.txt even manually. I open up info.txt, make a change, and click save, windows return an error
message: "The process cannot access the file because it is being used by another process." If I try to delete info.txt, the error message is "the action cant be completed because the file is open in AAA.exe".<br/>
When I run my (java) script that constantly updates info.txt, the file IO exception is: java.io.FileNotFoundException: info.txt (The process cannot access the file because it is being used by another process)<br/>
<br/>
<br/>
The part of feature in software AAA I am using is not yet fully developed to be publicly released. (Its a foreign software to my enterpise so I cant modify its behavior/source code.) The way it is accessing files locks up the file completely.
I thought of temperarily pointing away the reference to info.txt in the softwares profile files to allow a temparory edit, but this approach would fail because with AAA running (and I need it to), I cant make any change to any file its using.<br/>
<br/>
I consulted one of the main developer of software AAA. He acknowledges this is an issue right now, he would improve it in next release. But meanwhile I would like the set up to work. He told me he programs with .NET, and he provided a line that could help
me to get around my problem:<br/>
<br/>
FileStream fs = new FileStream(@"info.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);<br/>
<br/>
Supposedly, by supplying these arguments, this filestream are allowed to be shared on both writes and reads.<br/>
<br/>
<br/>
I am a java programmer, and Ive had no experience with .NET. However I did some research and learning, I realized his code is the syntex of C#, which runs in the CLR of .NET framework. I set up C# on my computer and studied some C# beginer tutorial, and
comes up with the following script, hoping to solve my problem.<br/>
<br/>
<br/>
If its really true that C# allows to change the access property of a file at a fundamental level, I would open up the filestream of info.txt, allowing it to be shared with both read and write. Once I do that, I can run my script of updating info.txt,
and then run the software AAA that keeps refreshing info.txt.<br/>
<br/>
Trying to achieve the above implementation, I come up with a C# script:<br/>
<br/>
================================================
<br/>
using System;<br/>
using System.IO;<br/>
<br/>
class CopyFeed<br/>
{<br/>
static void Main()<br/>
{<br/>
string fileName1 = "info.txt"; <br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre string fileName2 = "info2.txt";<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <br/>
FileStream fs = new FileStream(@fileName1, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);<br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre StreamWriter sw = null;<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <span style="white-space:pre // attempting to unlock part of the file, didnt work<br/>
<span style="white-space:pre <span style="white-space:pre /*<br/>
<span style="white-space:pre <span style="white-space:pre string contents = File.ReadAllText(@fileName2);<br/>
<span style="white-space:pre <span style="white-space:pre fs.Unlock(0, contents.Length * 2);<br/>
<span style="white-space:pre <span style="white-space:pre */<br/>
<br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre while (true)<br/>
<span style="white-space:pre <span style="white-space:pre {<br/>
<span style="white-space:pre <span style="white-space:pre Console.WriteLine("Copying Feed...");<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <span style="white-space:pre string contents = File.ReadAllText(@fileName2);<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <span style="white-space:pre try<br/>
<span style="white-space:pre <span style="white-space:pre {<br/>
<span style="white-space:pre <span style="white-space:pre sw = new StreamWriter(fs);<br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre sw.Write(contents);<br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre }<br/>
<span style="white-space:pre <span style="white-space:pre catch (Exception e)<br/>
<span style="white-space:pre <span style="white-space:pre {<br/>
<span style="white-space:pre <span style="white-space:pre Console.WriteLine("CopyFeed :: " + e.Message);<br/>
<span style="white-space:pre <span style="white-space:pre }<br/>
<span style="white-space:pre <span style="white-space:pre finally<br/>
<span style="white-space:pre <span style="white-space:pre {<br/>
<span style="white-space:pre <span style="white-space:pre if (sw != null)<span style="white-space:pre
<span style="white-space:pre {<span style="white-space:pre
<br/>
<span style="white-space:pre <span style="white-space:pre sw.Flush();<br/>
<span style="white-space:pre <span style="white-space:pre //sw.Close();<span style="white-space:pre
<br/>
<span style="white-space:pre <span style="white-space:pre }<br/>
<span style="white-space:pre <span style="white-space:pre }<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <span style="white-space:pre Console.WriteLine("Pausing For 2 sec...");<br/>
<span style="white-space:pre <span style="white-space:pre System.Threading.Thread.Sleep(2000);<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <span style="white-space:pre <span style="white-space:pre <span style="white-space:pre }<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <br/>
<span style="white-space:pre <br/>
}<br/>
}<br/>
<br/>
================================================<br/>
<br/>
<br/>
Supposedly, I will have my java script constantly updating info2.txt . Then I will have this C# script constantly copying the entire content of info2.txt into info.txt.<br/>



I wish to seek help with this post in 2 aspects:<br/>

<br/>
1. The above script is still buggy, because all it does is keep appending to the file info.txt. I wish to find a way that I can clear the content of file at the start of every iteration and just copy over the content of file2. Right now if I execute the script,
it will create a infinitely large file as time goes.<br/>
<br/>
<br/>
2. Based on the description above, is my solution going to work? Do I have any better option? (with software AAA behaviour staying the same) Is there even a way to get around it?<br/>
<br/>
<br/>
Ive been working on this problem for the past few days and am really stuck. I would sincerely thank any one who offers any insight!<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

<br/>
<br/>
<br/>

View the full article
 
Back
Top