473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Network Communication & File Locking

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

Apr 6 '07 #1
3 1947
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]
- mv*@spam.guard.caspershouse.com

<Da*********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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

Apr 6 '07 #2
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

On Apr 6, 5:38 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

<Davidher...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
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

Apr 6 '07 #3
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients...
1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
36
by: Thomas | last post by:
after spending countless hours trying, i give up and hope to get some help in here. on server1 i got the web myweb.com with my test.asp. in the test.asp, i'm trying to read a file from an UNC...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
1
by: intl04 | last post by:
Are there any problems with multiple user access to the same Access database on a shared network drive? I have 'shared' chosen for 'default open mode'. As for the record locking properties, I...
2
by: Leonardo D'Ippolito | last post by:
Hi! I have two .NET win apps that need to communicate on a TCP/IP network. 'App A' must ask 'app B' if it's allowed to do some task, and 'app B' must authorize or prohibit it. How can I do...
0
by: Grzegorz Kaczor | last post by:
Hello, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are...
3
by: musosdev | last post by:
Hi guys Okay, I've setup my projects to open and compile fine in VS2005 using FPSE and remote web, but it's *really* slow. So I thought I'd have a go at doing it the normal way, by loading from...
1
by: 33223 | last post by:
Local Area Network (LAN) Both an EtherNet (wire) network and a wireless network are referred to as a Local Area Network (LAN). A wireless network does not require hubs, switchers, or routers to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.