473,320 Members | 1,853 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,320 software developers and data experts.

FileSystemWatcher file in use problem

I need help. I have a directory I'm watching for creation of .TIF
files, whereupon creation, I need to launch a process (command line
exe) that converts the TIF file to a postscript file. (using
tif2ps.exe) The problem is, I can't open the .TIF file as long as my
application's running! tif2ps.exe always tells me "Cannot open file".

Here's the code in my Windows C# App:

private void button1_Click(object sender, System.EventArgs e)
{
FileSystemWatcher objFSW = new FileSystemWatcher();
objFSW.Path = "c:\\BizDocs\\vault";
objFSW.Filter = "*.tif";
label1.Text = "Watching " + objFSW.Path;
//Setup Events
objFSW.Created += new FileSystemEventHandler(FileCreated);
//Start Things up
objFSW.EnableRaisingEvents = true;
}
public static void FileCreated(object source, FileSystemEventArgs e)
{

#region WAIT FOR IT TO BE DONE CREATING...
Stream stream = null;
while (true)
{
try
{
if ((stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read,
FileShare.None)) != null)
break;
}
catch (IOException ex)
{
System.Threading.Thread.Sleep( 1000 );
}
}

if ( stream == null )
throw new ApplicationException("can't open file");

#endregion WAIT FOR IT TO BE DONE CREATING...

//give the output a stream to write to
System.IO.StreamWriter sw =
new StreamWriter(e.Name.ToString().Substring(0,e.Name. ToString().Length-3)
+ "ps");

//DEFINE A NEW PROCESS
System.Diagnostics.Process procTiff2ps = new
System.Diagnostics.Process);
System.Diagnostics.ProcessStartInfo i = new
System.Diagnostics.ProcessStartInfo();

i.FileName = "tiff2ps.exe";
i.Arguments = e.FullPath.ToString();//"C:\\BizDocs\\Vault\\401.TIF";
i.RedirectStandardOutput = true;
i.CreateNoWindow = true;
i.UseShellExecute = false;
procTiff2ps.StartInfo = i;
}

Help!

//START THE PROCESS
procTiff2ps.Start();
Jul 21 '05 #1
2 5635
Tom
Thats because you are opening and locking it. Why would you open the
file if the command line tool was going to open it.

ch***********@gmail.com (Charlie Kunkel) wrote in message news:<24*************************@posting.google.c om>...
I need help. I have a directory I'm watching for creation of .TIF
files, whereupon creation, I need to launch a process (command line
exe) that converts the TIF file to a postscript file. (using
tif2ps.exe) The problem is, I can't open the .TIF file as long as my
application's running! tif2ps.exe always tells me "Cannot open file".

Here's the code in my Windows C# App:

private void button1_Click(object sender, System.EventArgs e)
{
FileSystemWatcher objFSW = new FileSystemWatcher();
objFSW.Path = "c:\\BizDocs\\vault";
objFSW.Filter = "*.tif";
label1.Text = "Watching " + objFSW.Path;
//Setup Events
objFSW.Created += new FileSystemEventHandler(FileCreated);
//Start Things up
objFSW.EnableRaisingEvents = true;
}
public static void FileCreated(object source, FileSystemEventArgs e)
{

#region WAIT FOR IT TO BE DONE CREATING...
Stream stream = null;
while (true)
{
try
{
if ((stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read,
FileShare.None)) != null)
break;
}
catch (IOException ex)
{
System.Threading.Thread.Sleep( 1000 );
}
}

if ( stream == null )
throw new ApplicationException("can't open file");

#endregion WAIT FOR IT TO BE DONE CREATING...

//give the output a stream to write to
System.IO.StreamWriter sw =
new StreamWriter(e.Name.ToString().Substring(0,e.Name. ToString().Length-3)
+ "ps");

//DEFINE A NEW PROCESS
System.Diagnostics.Process procTiff2ps = new
System.Diagnostics.Process);
System.Diagnostics.ProcessStartInfo i = new
System.Diagnostics.ProcessStartInfo();

i.FileName = "tiff2ps.exe";
i.Arguments = e.FullPath.ToString();//"C:\\BizDocs\\Vault\\401.TIF";
i.RedirectStandardOutput = true;
i.CreateNoWindow = true;
i.UseShellExecute = false;
procTiff2ps.StartInfo = i;
}

Help!

//START THE PROCESS
procTiff2ps.Start();

Jul 21 '05 #2
Ok, I see why OP is opening the file, to check if it is fully created.
But OP have to be sure to close it after that. The change should be:

while (true)
{
using (Stream stream = File.Open(...))
{
if (stream != null)
break;
}
Thread.Sleep(1000);
}

Sunny

In article <63*************************@posting.google.com> , junkmale48
@hotmail.com says...
Thats because you are opening and locking it. Why would you open the
file if the command line tool was going to open it.

ch***********@gmail.com (Charlie Kunkel) wrote in message news:<24*************************@posting.google.c om>...
I need help. I have a directory I'm watching for creation of .TIF
files, whereupon creation, I need to launch a process (command line
exe) that converts the TIF file to a postscript file. (using
tif2ps.exe) The problem is, I can't open the .TIF file as long as my
application's running! tif2ps.exe always tells me "Cannot open file".

Here's the code in my Windows C# App:

private void button1_Click(object sender, System.EventArgs e)
{
FileSystemWatcher objFSW = new FileSystemWatcher();
objFSW.Path = "c:\\BizDocs\\vault";
objFSW.Filter = "*.tif";
label1.Text = "Watching " + objFSW.Path;
//Setup Events
objFSW.Created += new FileSystemEventHandler(FileCreated);
//Start Things up
objFSW.EnableRaisingEvents = true;
}
public static void FileCreated(object source, FileSystemEventArgs e)
{

#region WAIT FOR IT TO BE DONE CREATING...
Stream stream = null;
while (true)
{
try
{
if ((stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read,
FileShare.None)) != null)
break;
}
catch (IOException ex)
{
System.Threading.Thread.Sleep( 1000 );
}
}

if ( stream == null )
throw new ApplicationException("can't open file");

#endregion WAIT FOR IT TO BE DONE CREATING...

//give the output a stream to write to
System.IO.StreamWriter sw =
new StreamWriter(e.Name.ToString().Substring(0,e.Name. ToString().Length-3)
+ "ps");

//DEFINE A NEW PROCESS
System.Diagnostics.Process procTiff2ps = new
System.Diagnostics.Process);
System.Diagnostics.ProcessStartInfo i = new
System.Diagnostics.ProcessStartInfo();

i.FileName = "tiff2ps.exe";
i.Arguments = e.FullPath.ToString();//"C:\\BizDocs\\Vault\\401.TIF";
i.RedirectStandardOutput = true;
i.CreateNoWindow = true;
i.UseShellExecute = false;
procTiff2ps.StartInfo = i;
}

Help!

//START THE PROCESS
procTiff2ps.Start();

Jul 21 '05 #3

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

Similar topics

0
by: cxw0106 | last post by:
Hello, I have some weird problem with the FileSystemWatcher. I have developed an application that monitors a network directory for file changes of a certain file type. The program runs well for...
2
by: Phil396 | last post by:
I have a Windows service that use a FileSystemWatcher to scan incoming html for information. The process worked fine in a Windows Application and it also works fine in a Windows Service except...
1
by: Phil396 | last post by:
I have a windows service that uses a filesystemwatcher to wait for files and process them to a database. Sometimes a large group of files will be cut and paste for the filesystemwatcher to...
1
by: yogesh | last post by:
hello I getting problem in FileSystemWatcher , I had wrriten the code for the FileSystemWatcher , when i dropping the new file in the directory , the WatcherEdi_Created event get fired , but...
1
by: Long Tran | last post by:
Hello The DirectoryMonitor sample codes demonstrate the .NET Framework System.IO FileSystemWatcher object works nicely but display only short file name (for example test.bdrg become test~1.bdr)...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
3
by: savvy | last post by:
I'm using "Visual Studio 2005 Professional". I'm half way through in my project development. I've created a New Website in the Visual Studio 2005 and developing my project over there. When i wanted...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
0
by: valdas | last post by:
Hi, I have a problem in accessing "sqlServerCatalogNameOverwrites" section in app.config file. <configuration> <configSections> <section name="sqlServerCatalogNameOverwrites" ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.