Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Filesystemwatcher synchronous
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Vista
Miscellaneous
Thread ID:
01422062
Message ID:
01422152
Views:
34
>I am creating a PDF programmatically. The ActiveX component basically acts as a printer driver and I can't figure out how to know when it is done so I know the file exists for certain so I can read it into a byte array to store.
>
>I am trying now to do this with system.io.filesystemwatcher but I can't seem to get the syntax right
>

As far as I know, there isn't a nice easy way to only get notified when the file is closed (which is essentially what you want). Instead, I'd wait until the file has been created, then call out to another method which loops until you can get an exclusive lock on the new file (or times out), ex: (sorry, C# code and may not work as-is)
bool IsFileComplete(string fileName, TimeSpan timeout)
{
	bool complete = false;
	DateTime endTime = DateTime.Now.Add(timeout);
	
	while (endTime > DateTime.Now)
	{
		try
		{
			using (FileStream newFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
			{
				complete = true;
				break;
			}
		}
		catch (IOException) { }		

		System.Threading.Thread.Sleep(100);
	} 
	
	return complete;
}
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform