File I/O problem in C# (The process cannot access the file)

N

newms

The following thread is related to a file access problem that seems to be a
C# language problem. I have temporarily resolved it with vb.net as
described below and thought it might be helpful to anyone else experiencing
a similar issue. Or maybe someone can resolve this in C#...........
____________________________________________________________________________
_________________

Hi Scott, thanks a lot for your input. I did try using your syntax below in
C# but still not luck. I did get it fixed though...... what I did was
write my FileAppend function in a vb.net project and then inherited from
this vb.net assembly in my C# assembly. It works just fine uilizing the
vb.net FileOpen() function. This seems like a bug in C#........
____________________________________________________________________________
__________________


Do you know how the 3rd party app is querying the file? Is it watching
for notifications of file changes from the OS, or does it periodically check
the last modified date? When it queries it, does it write to it, or just
read?

Something like this might work. It adds the FileShare permissions, and
automatically flushes and closes the file when you're done.

using (StreamWriter w = File.Open(filePath, FileMode.Append,
FileAccess.Write, FileShare.ReadWrite))
{
w.WriteLine(dataItem.ToString());
}

____________________________________________________________________________
__________________



Hi Scott -

I'm having problems appending a line of text to a txt file. This txt file
is constantly being queried by a 3rd party app (Fourth Shift ERP system
"Recovery Server" www.fs.com) to see if any new records need to be
processed. I'm getting the error:

"The process cannot access the file
\"\\\\<server>\\fshift\\mfgsys\\rcvr.txt\" because it is being used by
another process."

Seems straight forward enough. When I turn off the 3rd party service, my
code can append data to the file no problem. I need my code to be able to
write to this file while the 3rd party app is also running. The interesting
part is that using the following "old-school" code in MSAccess works.
....and it works even while the 3rd party app is running:

Dim hwnd As Integer
hwnd= FreeFile()
Open "\\<server>\fshift\mfgsys\rcvr.txt" For Append As #hwnd
Print #hwnd, "Test"
Close #hwnd

Any thoughts on how I could simulate this old-school behavior or what I'm
missing in my c# code (below). (I have tried numerous other methods of
opening the file and also created a loop that would continue to attempt to
append the data, but nothing works....any ideas would be great. Once again,
it has to be something simple since the MSAccess code works perfectly well
every time.

FileInfo f = new FileInfo(filePath);

StreamWriter w = f.Open(FileMode.Append, FileAccess.Write);

w.WriteLine(dataItem.ToString());

w.Flush();

w.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top