Home All Groups Group Topic Archive Search About

Running command/removing file

Author
29 Mar 2005 2:46 PM
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

Author
29 Mar 2005 2:52 PM
Cor Ligthert
Mark,

Normally this should not be possible as long as you close the file before
that you remove it.

Cor
Author
29 Mar 2005 3:39 PM
Phill. W
"Mark" <m***@somewhere.com> wrote in message
news: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.

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.
Author
29 Mar 2005 4:21 PM
Herfried K. Wagner [MVP]
"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/>
Author
29 Mar 2005 5:00 PM
Mark
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/>
Author
29 Mar 2005 5:30 PM
Cor Ligthert
Mark,

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

Cor
Author
29 Mar 2005 7:37 PM
Mark
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
>
Author
29 Mar 2005 5:50 PM
Herfried K. Wagner [MVP]
"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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Mar 2005 10:56 PM
J L
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
>///
Author
30 Mar 2005 12:07 AM
Herfried K. Wagner [MVP]
"J L" <j***@marymonte.com> schrieb:
> 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?

AFAIK no.

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/>