Run new process as different user in vb.net

后端 未结 4 1813
孤独总比滥情好
孤独总比滥情好 2020-12-28 23:48

I\'m currently using a homegrown method to run a process as a different user in Vista, and I can\'t escape the feeling that\'s it hack-ish and less than ideal (in addition t

4条回答
  •  长发绾君心
    2020-12-29 00:09

    If you want to start an application with different credentials than the current running process, you can use the .Net Process class.

    this.Process = new Process();
    
    this.Process.StartInfo.Arguments = "Arguments";
    this.Process.StartInfo.FileName = "C:\your.exe";
    this.Process.StartInfo.UserName = "UserName";
    string password = "some password";
    
    this.Process.StartInfo.Password.Clear();
    foreach (char c in password)
    {
        this.Process.StartInfo.Password.AppendChar(c);
    }
    
    
    //allow the process to raise events
    this.Process.EnableRaisingEvents = true;
    this.Process.StartInfo.ErrorDialog = false;
    //Method for handling the exit event
    this.Process.Exited += new EventHandler(ApplicationProcess_Exited);
    
    //Set the application directory as the current working directory
    Environment.CurrentDirectory = System.IO.Directory.GetParent("C:\").ToString();
    
    if (this.Process.Start())
    {
        // Do something on start
    }
    

提交回复
热议问题