Running command/removing file

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

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?

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 said:
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'm assuming here that "command" means an external program being
run. If so, use [Process].Start() to get thigns going, then
[Process].WaitForExit() to see when it finishes.

HTH,
Phill W.
 
Mark said:
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).
 
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
 
Mark,

In my opinion has Phil given than the answer, did you try that?.

Cor
 
Mark said:
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
///
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top