Shell() = ? in .net

  • Thread starter Thread starter Henry Craven
  • Start date Start date
H

Henry Craven

Try as I might I can't get the following command to execute in .net

C:\winnt\System32\xcopy.exe D:\Data\Test D:\VB /e /d /h /o /v >
C:\Copyresults.txt

How do I get it to execute in Shell() Or what would be the equivalent
..net Commands ( note: the /o is important )

Tried
Shell,
System.Diagnostics.Process,
Process.Start
Process.Startinfo ( with and without RedirectStandardInput )

At this stage I'm up the creek and don't even know what the paddle
should look like.

All help gratefully accepted.
 
Hi Henry,

Two samples, the first when you call a shell extension and the second a
program

I hope this helps?

Cor

\\\
Dim p As New System.Diagnostics.ProcessStartInfo()
p.Verb = "print"
p.WindowStyle = ProcessWindowStyle.Hidden
p.FileName = "C:\filename.doc"
p.UseShellExecute = True
System.Diagnostics.Process.Start(p)
///
\\\
Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = False
p.StartInfo.Arguments = "\?"
p.StartInfo.WorkingDirectory = "C:\mydir"
p.StartInfo.FileName = "myProg.exe"
p.Start()
///
 
Thanks Cor.
But I know all that. ( note the list of tried solutions )
None of it works with the example I provided.
....and I specifically need to execute, or perform that operation.

( copied from a debug.writeline output and pasted into a command window
it executes as expected.)
 
Hi Henry,

You can try this

I hope this one helps?

Cor

\\\
Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.Arguments = "C:\Test1 C:\TestA\"
p.StartInfo.WorkingDirectory = "C:\windows\system32"
p.StartInfo.FileName = "xcopy"
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
Dim sw As New IO.StreamWriter("C:\Test2\mytest.txt")
Me.Close()
///
 
The problem it seems is when you start adding the xcopy switches, in which
case it works without redirection, but with redirection no copy is
performed.
 
Strange with me it does.
There is a little error in my sample, at the end should be

However that had only to do with the writing of the redirected file

\\\
sw.Write(sb.ToString)
sw.Close()
///
 
The writing of the output is fine here, but no copying takes place. What
does your Argumnet property look like?
 
Hi Carsten,
p.StartInfo.Arguments = "C:\Test1 C:\TestA\ /e /d /h /o /v "

The arguments prevent copying twice, I hope you recognized that?

Cor
 
Not sure what goes on here, but it doesn't work. In any case, the original
poster now has an answer, so I'll work this one out myself.

Thanks
 
* "Henry Craven said:
Try as I might I can't get the following command to execute in .net

C:\winnt\System32\xcopy.exe D:\Data\Test D:\VB /e /d /h /o /v >
C:\Copyresults.txt

How do I get it to execute in Shell() Or what would be the equivalent
.net Commands ( note: the /o is important )

Tried
Shell,
System.Diagnostics.Process,
Process.Start
Process.Startinfo ( with and without RedirectStandardInput )

At this stage I'm up the creek and don't even know what the paddle
should look like.

<URL:http://dotnet.mvps.org/dotnet/samples/miscsamples/downloads/RedirectConsole.zip>
 
Thanks guys. nearly there.

Adding the Params and Switches to the arguments works.
However: if I include the pipe for the output it fails again.

Not sure I completely understand with the Streamreader / Stringbuilder /
streamwriter here.
the test2.txt file is showing an output of numbers which I presume are
the ASCII output for what should be the Text Log but -how- do I convert
that to a meaningful log ?
 
Hi Henry,

Did you see the correction I did send for that on the message I made, this
should be on the end and delete that me.close.
\\\
sw.Write(sb.ToString)
sw.Close()
///
Cor
 
Hi CT,

Oooogh

Maybe it is that, that "> output file" has to be not included in the
parameters

Cor
 
Hi Henry,

Same message as to CT

Maybe it is that, that "> output file" that has not to be included in the
parameters

Cor
 
Thanks Cor, yes I saw it, and realised it was missing as I had it in the
version of the code I'd been trying previously to stream the Switches.

Testing also picked up the fact that it won't accept piping the result
to the text file.

changing your code to:
Dim sinput As String = sr.ReadLine()
will give me one line of readable output, but I don't know how to
capture the others so I get one line of readable, and then just numbers.
 
Solved: Thanks all:

Dim ProcID As Integer

Dim sSource As String = "D:\Data\Test"

Dim sDest As String = "D:\VB"

Dim sShell As String = "E:\winnt\System32\xcopy.exe"

Dim s As String = ""

'Try

Dim p As New Process

p.StartInfo.UseShellExecute = False

p.StartInfo.RedirectStandardOutput = True

p.StartInfo.Arguments = "D:\Data\Test D:\VB /e /d /h /o /v"

p.StartInfo.WorkingDirectory = "E:\winnt\system32"

p.StartInfo.FileName = "xcopy"

p.Start()

Dim sr As IO.StreamReader = p.StandardOutput

Dim sb As New System.Text.StringBuilder("")

Dim sinput As String = ""

Do Until sinput = "-1"

sb.Append(sr.ReadLine() & ControlChars.CrLf)

sinput = sr.Read

Loop

Dim sw As New IO.StreamWriter("C:\test2.txt")

sw.Write(sb.ToString)

Me.Close()
 
Back
Top