|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Running command/removing fileI have a command that needs to be run on a file and then once done the file
needs to be removed. Doing this, some of the time I receive an error that it is already being accessed. I have tried putting a sleep after the command, and that works - not always guaranteed to work though, but I would rather do it another way. How can I set it so that it will watch to see if the file is still being accessed and if not delete otherwise wait until it is not being accessed? Thanks in advance. Mark Mark,
Normally this should not be possible as long as you close the file before that you remove it. Cor "Mark" <m***@somewhere.com> wrote in message I'm assuming here that "command" means an external program beingnews:eZ09I4GNFHA.2384@tk2msftngp13.phx.gbl... > I have a command that needs to be run on a file and then once done > the file needs to be removed. Doing this, some of the time I receive > an error that it is already being accessed. run. If so, use [Process].Start() to get thigns going, then [Process].WaitForExit() to see when it finishes. HTH, Phill W. "Mark" <m***@somewhere.com> schrieb: Post the important parts of your code (opening, closing, and deleting the >I have a command that needs to be run on a file and then once done the file >needs to be removed. Doing this, some of the time I receive an error that >it is already being accessed. I have tried putting a sleep after the >command, and that works - not always guaranteed to work though, but I would >rather do it another way. How can I set it so that it will watch to see if >the file is still being accessed and if not delete otherwise wait until it >is not being accessed? file). -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I don't open the file, I am just sending it to a printer with a shell
command. Here is the code: =================== Shell("lpr -S 10.1.1.1 -P " & printerName & " " & Chr(34) & fileName & Chr(34)) If File.Exists(fileName) Then File.Delete(fileName) End If ==================== Thanks. Mark Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:uRZJQsHNFHA.2748@TK2MSFTNGP09.phx.gbl... > "Mark" <m***@somewhere.com> schrieb: >>I have a command that needs to be run on a file and then once done the >>file needs to be removed. Doing this, some of the time I receive an error >>that it is already being accessed. I have tried putting a sleep after the >>command, and that works - not always guaranteed to work though, but I >>would rather do it another way. How can I set it so that it will watch to >>see if the file is still being accessed and if not delete otherwise wait >>until it is not being accessed? > > Post the important parts of your code (opening, closing, and deleting the > file). > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> Mark,
In my opinion has Phil given than the answer, did you try that?. Cor Yes, it did but I did have to do some other things to get it to work. For
those interested here is what I have: Dim lprProcess As Process = New Process 'set the filename (process) and the command line arguments lprProcess.StartInfo.FileName = "lpr" lprProcess.StartInfo.Arguments = "-S 10.1.1.1 -P " & printerName & " " & Chr(34) & fileName & Chr(34) 'start the process in a hidden window lprProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden lprProcess.StartInfo.CreateNoWindow = True lprProcess.Start() 'if the process doesn't complete within 1 minute, kill it lprProcess.WaitForExit(60000) If Not lprProcess.HasExited Then lprProcess.Kill() End If lprProcess.Close() If File.Exists(fileName) Then File.Delete(fileName) End If Information found here: http://www.devx.com/dotnet/Article/7914 Thanks. Mark Show quoteHide quote "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:ebkNPUINFHA.940@TK2MSFTNGP09.phx.gbl... > Mark, > > In my opinion has Phil given than the answer, did you try that?. > > Cor > "Mark" <m***@somewhere.com> schrieb: Untested:>I don't open the file, I am just sending it to a printer with a shell > command. Here is the code: > =================== > Shell("lpr -S 10.1.1.1 -P " & printerName & " " & Chr(34) & fileName & > Chr(34)) > If File.Exists(fileName) Then > File.Delete(fileName) > End If \\\ Imports System.Diagnostics .. .. .. Dim p As Process = Process.Start(...) p.WaitForExit() If File.Exists(FileName) Then File.Delete(FileName) End If /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> In VB6 I used FindExecutable to see if a file had an associated
program before trying to shell out. Is there a similar function in VB.Net? John On Tue, 29 Mar 2005 19:50:06 +0200, "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote: Show quoteHide quote >"Mark" <m***@somewhere.com> schrieb: >>I don't open the file, I am just sending it to a printer with a shell >> command. Here is the code: >> =================== >> Shell("lpr -S 10.1.1.1 -P " & printerName & " " & Chr(34) & fileName & >> Chr(34)) >> If File.Exists(fileName) Then >> File.Delete(fileName) >> End If > >Untested: > >\\\ >Imports System.Diagnostics >. >. >. >Dim p As Process = Process.Start(...) >p.WaitForExit() >If File.Exists(FileName) Then > File.Delete(FileName) >End If >/// "J L" <j***@marymonte.com> schrieb: AFAIK no.> In VB6 I used FindExecutable to see if a file had an associated > program before trying to shell out. Is there a similar function in > VB.Net? Determining a file's associated application <URL:http://dotnet.mvps.org/dotnet/faqs/?id=findassociatedapp&lang=en> -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> |
|||||||||||||||||||||||