Solved

vb.net and filepath for new process creation

Posted on 2014-12-09
39
24 Views
Last Modified: 2016-06-23
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
Comment
Question by:derek7467
  • 18
  • 15
  • 4
39 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40489579
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
 

Author Comment

by:derek7467
ID: 40489585
Im trying to use this line:
p.StartInfo.FileName = PsExecPath

Not the full path
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40489597
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
 

Author Comment

by:derek7467
ID: 40489621
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40489628
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40489633
>>MessageBox.Show(PsExecPath);
That is C#, I think the vb equivalent would be
MessageBox.Show PsExecPath
0
 

Author Comment

by:derek7467
ID: 40489662
It reads the right path and file
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40489673
Is the cannot find file the error message ?
Do you have read permission on the directory ?
0
 

Author Comment

by:derek7467
ID: 40489698
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40489707
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
 

Author Comment

by:derek7467
ID: 40489713
thanks Mike. had to replace the , with a + and it worked.
0
 

Author Comment

by:derek7467
ID: 40489718
Actually, it didnt work.  Still get Cannot find file when i use:

p.StartInfo.FileName = PsExecPath
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40489773
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
 

Author Comment

by:derek7467
ID: 40489802
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40489851
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
 

Author Comment

by:derek7467
ID: 40489865
same, file not found
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40489892
Which message box, though.  The exception one, or the outer one that explicitly checked for the file?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40490571
>>Ok, so now the msgbox reads correctly,

But you have said that already with your previous code.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:derek7467
ID: 40491181
Neither one shows up.  It acts like its working but the code never fires off.  No error messages though using your code.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491234
??
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
 

Author Comment

by:derek7467
ID: 40491302
I assume i would get the error inside of my try-catch and if statement.

file not found
0
 

Author Comment

by:derek7467
ID: 40491306
I mean, duplicate the code on your side, you should get the same results i'm getting
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491336
p.StartInfo.FileName = PsExecPath
MessageBox.Show(PsExecPath)

Please supply a screenshot of the message box output
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491348
>>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
 

Author Comment

by:derek7467
ID: 40491350
K, i blacked out my username.  Not sure why you think im making this up.

msgbox
0
 

Author Comment

by:derek7467
ID: 40491353
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491393
>>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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491527
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491546
By the way, psexec is a third party app, not part  of (some/all?) windows installations.
0
 

Author Comment

by:derek7467
ID: 40491552
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491579
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
 

Author Comment

by:derek7467
ID: 40491664
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
 

Author Comment

by:derek7467
ID: 40491674
Different directory works fine.  Ugh i really wanted to use appdata.  No problem, i use a different generic directory.  Thanks for the help!!!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491683
I know it is from sysinternals.  They were taken over by Microsoft some years back.
0
 

Author Comment

by:derek7467
ID: 40491742
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40491822
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
 

Accepted Solution

by:
derek7467 earned 0 total points
ID: 40491826
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now