我如何作为另一个用户运行应用程序

时间:2012-02-17 11:07:52

标签: c#

我知道这个问题已被提出但我找不到任何答案。我有这个代码我正在尝试与特定用户运行一个应用程序,但是即使文件存在也无法找到该文件的错误。

    static void Main(string[] args)
    {
        System.Diagnostics.ProcessStartInfo myProcess = new System.Diagnostics.ProcessStartInfo("cinegy.exe");
        myProcess.WorkingDirectory =Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+ "\\Cinegy\\Cinegy Workflow 8.5.8\\";
        System.Security.SecureString password = new System.Security.SecureString();

        string uspw = "mypass";

        foreach (char c in uspw)
        {
            password.AppendChar(c);
        }
        myProcess.UserName = "myuser";
        myProcess.Password = password;
        myProcess.Domain = "mydomain";
        myProcess.UseShellExecute = false;
        try
        {
            System.Diagnostics.Process.Start(myProcess);
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
}

由于

错误是|系统找不到指定的文件|

1 个答案:

答案 0 :(得分:2)

如果您使用

UseShellExecute = false

它会忽略WorkingDirectory

您可以将UseShellExecute设置为true并具有cmd shell。或者,您将流程的位置添加到正在运行的流程的路径中:

string path = System.Environment.GetEnvironmentVariable("path");
path += ";" + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\Cinegy\\Cinegy Workflow 8.5.8\\";
System.Environment.SetEnvironmentVariable("path", path);