Question Process.Start with local user

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Many times I have startet a different application from my programs with other users, but now I can't get it working with a local user

Here's the situation: On a server we have created a local user (not administrator), that is allowed to run the program needed. If we log in as a different user, right click on the file and selects "run as" and writes the local user name and password, the program runs as it should

But when I do the same in my Vb.net program it fails with "The directory name is invalid"
Here's the code I have:

VB.NET:
Imports System.Security

Public Class frmRunAsAdministrator

    Private password As New SecureString
    Private user As String = "superman"
    Private fileName As String = "C:\Temp\TheTimer.exe"
    Private domain As String = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            SetPassWord()
            Process.Start(fileName, user, password, domain)
        Catch ex As Exception
            Dim msg As String = "Fejl opstod ved start af program" & vbCrLf & ex.Message

            If Not ex.InnerException Is Nothing Then
                msg = msg & vbCrLf & ex.InnerException.ToString
            End If

            MsgBox(msg, MsgBoxStyle.Critical)
        Finally
            Application.Exit()
        End Try
    End Sub

    Private Sub SetPassWord()
        Dim pass As String = "MySuperPassword"
        Dim num As Integer = 0
        Dim length As Integer = pass.Length

        Do While (num < length)
            Dim c As Char = pass.Chars(num)
            password.AppendChar(c)
            num += 1
        Loop
    End Sub

End Class

If I use our domain administrator or a different domain user (not Admin) it works just fine. I have tried setting domain both to nothing and to the name of the server it runs on, all with the same result

TheTimer.exe is just a vb.net application that doens't require anything and it is using Framework 2.xxx. The directory it is in, is a directory everybody has access to

I have done a lot of searching for the error message, but nothing useful has come up yet

Anybody who has any idea what the problem could be?
 

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have also tried to login on the server with the local username and password. From here there is no problems running C:\Temp\TheTimer.exe
But running the above program still gives me the same error
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
If there was a problem with user/password you would get Win32Exception "Logon failure", so that must be ok.

Here's what to do: Create a ProcessStartInfo instead and set the properties (username, password etc) and in particular set WorkingDirectory property (even if you're using default %SYSTEMROOT%\system32), usually W.D should be set to same folder the executable is in and can be extracted with IO.Path.GetDirectoryName method. UseShellExecute property must also be False. Then Process.Start your info object.

About this code:
VB.NET:
        Dim num As Integer = 0
        Dim length As Integer = pass.Length

        Do While (num < length)
            Dim c As Char = pass.Chars(num)
            password.AppendChar(c)
            num += 1
        Loop
it can be reduced to this:
VB.NET:
For Each c As Char In pass
    password.AppendChar(c)
Next
 

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Thanks for the tip, unfortunately I have already tried setting the WorkingDirectory as you say with UseShellExecute set to false. Gives me the same result

And thanks for the tup on how to reduce the code, can't believe I didn't see that myself :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
Usually that error means path is wrong, or WorkingDirectory wrong or not set, but it may also occur if user doesn't have access (permission) to the folder. I don't have a domain set up and wouldn't know which value you should use for this, so strictly it would be possible you were using a valid account though not an account that had access. Basically these are the three requirements and causes for that error.
is a directory everybody has access to
"access" can mean a lot, since you're executing the Read & Execute rights is required.
 

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Let's put it this way:
If I log onto the computer as the user I'm trying to execute a program as through my program, then I have no problems going to the \temp\ folder and execute TheTimer.exe
So the user has all the rights needed. The user also has write permission to the folder, even though it's not needed
 
Top Bottom