Network Communication & File Locking

D

Davidhere40

I'm trying to lock a text file, read its contents, make the text file
empty, then unlock it. I haven't been successful at it yet. The
following code is what I've tried, with no success, because the file
stays locked even after the function finishes.

FileStream fs = new FileStream(fullPath, FileMode.Open,
FileAccess.ReadWrite, FileShare.Delete);

StreamReader streamReader = new StreamReader(fs);
content = streamReader.ReadToEnd();
streamReader.Dispose();

File.Delete(fullPath);
File.Create(fullPath);

fs.Dispose();

The real reason I'm implementing this is below if it matters:
I have to implement a system that allows one computer to tell multiple
other computers connected to a single router, that new files have been
put on their computers. I decided that the best way to do it was to
have a text file that allows me to tell the computer the path of the
new files. I did it this way, because I will have to later port the
program over to java later to save money on not needing to buy
windows, and I'm pretty sure that this method is portable.

Thanks
Dave
 
N

Nicholas Paldino [.NET/C# MVP]

Um, this kind of brings up a question, why do this in .NET in the first
place if you know you are inevitably going to port to Java? Why not just do
it in Java in the first place?

Also, I would look into some sort of remoting technology to notify
clients when something has happened. Using a file drop is a little
unreliable, if you ask me.
 
D

Davidhere40

I knew someone was going to ask that. I should have answered it before
hand. The reason I don't want to do it in Java, ajax and asp.net from
the get go is because I have to setup a huge system in about a month
to guarantee myself a huge royalties. I've never been able to do the
type of quality and speed of development in java that I can do in c#.
Once I gave into a team of programmers in India that couldn't do shit,
that wanted to do a similar app as a web app on the first go. As of
now, its been well over a year and their system sucks, and still isn't
even close to done. In c# I can finish the whole thing in about a
month, and then hire someone that can make it cross platform for me in
a few months.

That's long answer :)

My fear in using remoting technology is that I'll be dependent on
microsoft technology, and not be able to do the same thing in java on
a linux box. But if you say I can and think a fairly experience
programmer can pick it and implement it in no time, then point me to
it.

Thanks
Dave

Um, this kind of brings up a question, why do this in .NET in the first
place if you know you are inevitably going to port to Java? Why not just do
it in Java in the first place?

Also, I would look into some sort of remoting technology to notify
clients when something has happened. Using a file drop is a little
unreliable, if you ask me.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I'm trying to lock a text file, read its contents, make the text file
empty, then unlock it. I haven't been successful at it yet. The
following code is what I've tried, with no success, because the file
stays locked even after the function finishes.
FileStream fs = new FileStream(fullPath, FileMode.Open,
FileAccess.ReadWrite, FileShare.Delete);
StreamReader streamReader = new StreamReader(fs);
content = streamReader.ReadToEnd();
streamReader.Dispose();


The real reason I'm implementing this is below if it matters:
I have to implement a system that allows one computer to tell multiple
other computers connected to a single router, that new files have been
put on their computers. I decided that the best way to do it was to
have a text file that allows me to tell the computer the path of the
new files. I did it this way, because I will have to later port the
program over to java later to save money on not needing to buy
windows, and I'm pretty sure that this method is portable.
Thanks
Dave
 
M

Morten Wennevik [C# MVP]

Hi Dave,

I'm not exactly sure what happens here, but File.Create seems to lock the file until the application ends.
As an alternative solution to empty the file you can do this instead

using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
using (StreamReader sr = new StreamReader(fs))
{
s = sr.ReadToEnd();
}
using (FileStream fs2 = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
}
}

Any class implementing Close and/or Dispose can be enclosed in a using statement, ensuring the objects are properly disposed/closed as well as enhancing readability.
 

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