Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

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

0
derek7467
Asked:
derek7467
  • 18
  • 15
  • 4
1 Solution
 
AndyAinscowCommented:
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
0
 
derek7467Author Commented:
Im trying to use this line:
p.StartInfo.FileName = PsExecPath

Not the full path
0
 
AndyAinscowCommented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
derek7467Author Commented:
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

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

Now check the path and file do exist.
0
 
AndyAinscowCommented:
>>MessageBox.Show(PsExecPath);
That is C#, I think the vb equivalent would be
MessageBox.Show PsExecPath
0
 
derek7467Author Commented:
It reads the right path and file
0
 
AndyAinscowCommented:
Is the cannot find file the error message ?
Do you have read permission on the directory ?
0
 
derek7467Author Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

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

p.StartInfo.FileName = PsExecPath
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
derek7467Author Commented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
derek7467Author Commented:
same, file not found
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Which message box, though.  The exception one, or the outer one that explicitly checked for the file?
0
 
AndyAinscowCommented:
>>Ok, so now the msgbox reads correctly,

But you have said that already with your previous code.
0
 
derek7467Author Commented:
Neither one shows up.  It acts like its working but the code never fires off.  No error messages though using your code.
0
 
AndyAinscowCommented:
??
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
0
 
derek7467Author Commented:
I assume i would get the error inside of my try-catch and if statement.

file not found
0
 
derek7467Author Commented:
I mean, duplicate the code on your side, you should get the same results i'm getting
0
 
AndyAinscowCommented:
p.StartInfo.FileName = PsExecPath
MessageBox.Show(PsExecPath)

Please supply a screenshot of the message box output
0
 
AndyAinscowCommented:
>>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).
0
 
derek7467Author Commented:
K, i blacked out my username.  Not sure why you think im making this up.

msgbox
0
 
derek7467Author Commented:
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...
0
 
AndyAinscowCommented:
>>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) ?
0
 
AndyAinscowCommented:
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.
0
 
AndyAinscowCommented:
By the way, psexec is a third party app, not part  of (some/all?) windows installations.
0
 
derek7467Author Commented:
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?
0
 
AndyAinscowCommented:
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)
0
 
derek7467Author Commented:
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
0
 
derek7467Author Commented:
Different directory works fine.  Ugh i really wanted to use appdata.  No problem, i use a different generic directory.  Thanks for the help!!!
0
 
AndyAinscowCommented:
I know it is from sysinternals.  They were taken over by Microsoft some years back.
0
 
derek7467Author Commented:
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."
0
 
AndyAinscowCommented:
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")
0
 
derek7467Author Commented:
Figured it out:

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

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 18
  • 15
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now