Link to home
Start Free TrialLog in
Avatar of derek7467
derek7467

asked on

vb.net and filepath for new process creation

I wrote the below method to utilize PsExec to launch task manager on a remote workstation.  I can use this tool successfully when use the commented line (the P.StartInfo.Filename).  It works fine.  When i try and use a concatenated string (the Dim PsExecPath variable), it errors out with "cannot find file".  Can anyone tell me why?

 Dim targetpc As String = TextBox1.Text
        Dim pstools As String = "C:\SWDEPOT\SD Remote Tool\PSTools"
        Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim PsExecPath = path + "\SD Remote Tools\PsExec.exe"
        'Try
        Dim p As New Process()
        'p.StartInfo.FileName = "C:\SWDEPOT\SD Remote Tool\PSTools\PsExec.exe"
        p.StartInfo.FileName = PsExecPath
        p.StartInfo.Arguments = "-s" & " " & "-i" & " " & "-d" & " " & "\\" & targetpc & " " & Chr(34) & "c:\windows\system32\taskmgr.exe" & Chr(34)
        p.Start()
        p.StartInfo.CreateNoWindow = True
        p.WaitForExit()
        'Catch ex As Exception
        '    MsgBox("Remote Workstation Unavailable", , "Information")
        'End Try

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

p.StartInfo.FileName = @"C:\SWDEPOT\SD Remote Tool\PSTools\PsExec.exe"

The \ is part of an 'escape' character.  Use either the @ to force that to be ignored or change all \ to \\ in the string
Avatar of derek7467
derek7467

ASKER

Im trying to use this line:
p.StartInfo.FileName = PsExecPath

Not the full path
Same thing as I said before.

The \ is part of an 'escape' character.  Use either the @ to force that to be ignored or change all \ to \\ in the string
Same error using the newly modified code:

Dim targetpc As String = TextBox1.Text
               Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim PsExecPath = path + "\\SD Remote Tools\\PsExec.exe"
        Dim p As New Process()
         p.StartInfo.FileName = PsExecPath
        p.StartInfo.Arguments = "-s" & " " & "-i" & " " & "-d" & " " & "\\" & targetpc & " " & Chr(34) & "c:\windows\system32\taskmgr.exe" & Chr(34)
        p.Start()
        p.StartInfo.CreateNoWindow = True
        p.WaitForExit()
     

Open in new window

Dim PsExecPath = path + "\\SD Remote Tools\\PsExec.exe"
MessageBox.Show(PsExecPath);
        Dim p As New Process()

Now check the path and file do exist.
>>MessageBox.Show(PsExecPath);
That is C#, I think the vb equivalent would be
MessageBox.Show PsExecPath
It reads the right path and file
Is the cannot find file the error message ?
Do you have read permission on the directory ?
yes it is and yes i do, because if i do it like p.StartInfo.FileName = "C:\Users\jdoe\AppData\Local\SD Remote Tools\PsExec.exe" it works fine
Try it this way:
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim PsExecPath = System.IO.Path.Combine(path, "\SD Remote Tools\PsExec.exe")

Open in new window

thanks Mike. had to replace the , with a + and it worked.
Actually, it didnt work.  Still get Cannot find file when i use:

p.StartInfo.FileName = PsExecPath
The comma wasn't a typo.  I'm passing two parameters into Path.Combine().

The problem is the leading backslash in the second param.  Try this out instead:
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim PsExecPath = System.IO.Path.Combine(path, "SD Remote Tools\PsExec.exe") ' <--- NOTE the leading backslash before "SD Remote Tools" has been REMOVED
MessageBox.Show(PsExecPath)

Open in new window

Ok, so now the msgbox reads correctly, but when i try to call it in the below line, it still says "File Not Found"
p.StartInfo.FileName = PsExecPath

 Dim p As New Process()
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim PsExecPath = System.IO.Path.Combine(path, "SD Remote Tools\PsExec.exe")
        MsgBox(PsExecPath)
             p.StartInfo.FileName = PsExecPath
            p.StartInfo.Arguments = "-s" & " " & "-i" & " " & "-d" & " " & "\\" & targetpc & " " & Chr(34) & "c:\windows\system32\taskmgr.exe" & Chr(34)
            p.Start()
            p.StartInfo.CreateNoWindow = True
            p.WaitForExit()

Open in new window

What happens with this?
        Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim PsExecPath = System.IO.Path.Combine(path, "SD Remote Tools\PsExec.exe")
        If System.IO.File.Exists(PsExecPath) Then
            Try
                Dim p As New Process
                p.StartInfo.FileName = PsExecPath
                p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(PsExecPath)
                p.StartInfo.Arguments = "-s -i -d \\" & targetpc & " " & Chr(34) & "c:\windows\system32\taskmgr.exe" & Chr(34)
                p.Start()
                p.StartInfo.CreateNoWindow = True
                p.WaitForExit()
            Catch ex As Exception
                MessageBox.Show(ex.ToString, "Process.Start() Failed")
            End Try
        Else
            MessageBox.Show(PsExecPath, "File Not Found")
        End If

Open in new window

same, file not found
Which message box, though.  The exception one, or the outer one that explicitly checked for the file?
>>Ok, so now the msgbox reads correctly,

But you have said that already with your previous code.
Neither one shows up.  It acts like its working but the code never fires off.  No error messages though using your code.
??
Where do you get this error message about the file not found?  Can you supply a screenshot of it?

>>but the code never fires off.
You explicitly hide any window the exe has when running, so how do you check that the code is running?
p.StartInfo.CreateNoWindow = True
I assume i would get the error inside of my try-catch and if statement.

User generated image
I mean, duplicate the code on your side, you should get the same results i'm getting
p.StartInfo.FileName = PsExecPath
MessageBox.Show(PsExecPath)

Please supply a screenshot of the message box output
>>I mean, duplicate the code on your side, you should get the same results i'm getting

Why should the results be the same.  They may be but then again they may differ.  (Different systems and settings and possibly OP system versions).
K, i blacked out my username.  Not sure why you think im making this up.

User generated image
Well, psexec is psexec and i would assume your on win7, so copying my code should produce similar results if youre on a domain infrastructure.  I guess there are a lot of ifs there...
>>Not sure why you think im making this up.

I'm not saying that, I am just very puzzled because nothing is obviously wrong when looking at the code, but one doesn't see everything there.
Does you name have any 'unusual' characters such as accents (simple yes/no will suffice) ?
I still suspect something to do with directory permissions as it is in the 'users' directory.
Can you create a folder elsewhere, copy the psexec.exe to that new folder and test your code against that different path.
By the way, psexec is a third party app, not part  of (some/all?) windows installations.
No accents on my username.  But if i use an absolute path to the same area in that msgbox, it works fine, so i doubt its permissions issue?
I'd still try copying it to a 'normal' folder.  The users folder area can be an odd place - windows uses some smoke and mirrors to show paths as one thing but in reality they are something else.  (eg.  Foreign language versions)
psexec is a 3rd party tool by sysinternals.  The service it connects to is on every windows xp/7 workstation.

Ill try a different directory
Different directory works fine.  Ugh i really wanted to use appdata.  No problem, i use a different generic directory.  Thanks for the help!!!
I know it is from sysinternals.  They were taken over by Microsoft some years back.
So im trying to change the below to read from the new location and i get an integer error can you help?

Dim location As New DirectoryInfo(Path.Combine(Environment.GetFolderPath("C:\SWDEPOT"), "SD Remote Tools"))
        Dim database As New FileInfo(Path.Combine(location.FullName, "PsExec.exe"))
        Dim fileStream As FileStream

Open in new window


Error i get is "Conversion from string "C:\SWDEPOT" to type 'Integer' is not valid."
Looking up Environment.GetFolderPath in help:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath%28v=vs.110%29.aspx
and one sees it is for retrieving a path to a special system folder and uses an int (enumerated value) to identify the special folder.  "C:\SWDEPOT" is not a special system folder

You probably want to use
"C:\SWDEPOT"
instead of
Environment.GetFolderPath("C:\SWDEPOT")
ASKER CERTIFIED SOLUTION
Avatar of derek7467
derek7467

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial