473,407 Members | 2,315 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,407 software developers and data experts.

Quick C# Question

I have a file being copied to my server from another server on the network.
My program needs to work with this file when it is received on my server.
The problem I have is that when I go to process the file it may still be
being written to the disk and thus I cannot get a valid handle on it to
perform the necessary operations I need to do.

The code I inherited uses a sleep on the thread that the code is running in.
I am looking for a more elegant solution that does not put the thread to
sleep for some static amount of time. Any suggestions would be greatly
appreciated. Thank you.
Nov 16 '05 #1
6 981
Maybe, you can use File's lastmodified/created date to determine whether the
file is completely written on it or not

Alternatively, you could use FileSystemWatcher class which gives notification
when a new file is written/deleted etc

Does this help ?

Kalpesh
Nov 16 '05 #2
Hi McFly,

Maybe FileSystemWatcher will help you, although I'm not familiar with it
nor know if it will simplify trying to read the file while it is being
written.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
I am using a FileSystemWatcher and it raises the events before I can get a
handle to the file. I think this is either because the file is still being
written or the disk is doing a lasy write. Thank you for your input.

"<Kalpesh/>" <ka*****@disc.microsoft.com> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
Maybe, you can use File's lastmodified/created date to determine whether the file is completely written on it or not

Alternatively, you could use FileSystemWatcher class which gives notification when a new file is written/deleted etc

Does this help ?

Kalpesh

Nov 16 '05 #4
I am using a FileSystemWatcher and it raises the events before I can get a
handle to the file. I think this is either because the file is still being
written or the disk is doing a lasy write. Thank you for your input.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opshiijbhsklbvpo@pbn_computer...
Hi McFly,

Maybe FileSystemWatcher will help you, although I'm not familiar with it
nor know if it will simplify trying to read the file while it is being
written.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #5
This is the code I wrote for that. See if it is of any use for you.

/// <summary>
/// Makes sure that the file is fully saved by trying to open it for
ReadWrite.
/// Generates exception if fails to do this within given period of
time.
/// </summary>
/// <param name="fi">FileInfo of the file to watch</param>
/// <param name="toSeconds">timeout in seconds</param>
/// <exception cref="System.Exception">Timeout: file is locked by
the writing process for more than specified time</exception>
static public void WaitForFullySaved (FileInfo fi, int toSeconds)
{
int timeout;
FileStream fs;

timeout = toSeconds * 1000;
while (timeout > 0)
try
{
fs = fi.Open (FileMode.Open, FileAccess.ReadWrite);
fs.Close ();
break;
}
catch (IOException)
{
System.Threading.Thread.Sleep (500);
timeout -= 500;
}
if (timeout <= 0)
throw (new Exception (String.Format (
"File {1} is locked by the writing process for more than
{2} seconds",
fi.Name, toSeconds)));
}

Eliyahu

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
I have a file being copied to my server from another server on the network. My program needs to work with this file when it is received on my server.
The problem I have is that when I go to process the file it may still be
being written to the disk and thus I cannot get a valid handle on it to
perform the necessary operations I need to do.

The code I inherited uses a sleep on the thread that the code is running in. I am looking for a more elegant solution that does not put the thread to
sleep for some static amount of time. Any suggestions would be greatly
appreciated. Thank you.

Nov 16 '05 #6
This is what I was looking for. Thank you.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
This is the code I wrote for that. See if it is of any use for you.

/// <summary>
/// Makes sure that the file is fully saved by trying to open it for ReadWrite.
/// Generates exception if fails to do this within given period of
time.
/// </summary>
/// <param name="fi">FileInfo of the file to watch</param>
/// <param name="toSeconds">timeout in seconds</param>
/// <exception cref="System.Exception">Timeout: file is locked by
the writing process for more than specified time</exception>
static public void WaitForFullySaved (FileInfo fi, int toSeconds)
{
int timeout;
FileStream fs;

timeout = toSeconds * 1000;
while (timeout > 0)
try
{
fs = fi.Open (FileMode.Open, FileAccess.ReadWrite);
fs.Close ();
break;
}
catch (IOException)
{
System.Threading.Thread.Sleep (500);
timeout -= 500;
}
if (timeout <= 0)
throw (new Exception (String.Format (
"File {1} is locked by the writing process for more than {2} seconds",
fi.Name, toSeconds)));
}

Eliyahu

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
I have a file being copied to my server from another server on the

network.
My program needs to work with this file when it is received on my server. The problem I have is that when I go to process the file it may still be
being written to the disk and thus I cannot get a valid handle on it to
perform the necessary operations I need to do.

The code I inherited uses a sleep on the thread that the code is running

in.
I am looking for a more elegant solution that does not put the thread to
sleep for some static amount of time. Any suggestions would be greatly
appreciated. Thank you.


Nov 16 '05 #7

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

Similar topics

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.