C#:加载漫游配置文件并以用户身份执行程序

时间:2023-01-16 15:41:28

In an application I need to execute other programs with another user's credentials. Currently I use System.Diagnostics.Process.Start to execute the program:

在应用程序中,我需要使用其他用户的凭据执行其他程序。目前我使用System.Diagnostics.Process.Start来执行程序:

public static Process Start(
   string fileName,
   string arguments,
   string userName,
   SecureString password,
   string domain
)

However this function does not load the roaming profile from the net - which is required.

但是,此功能不会从网络加载漫游配置文件 - 这是必需的。

I could use "runas /profile ..." to load the profile and execute the command, but that would ask for a password. There must be an more elegant way...

我可以使用“runas / profile ...”加载配置文件并执行命令,但这会要求输入密码。必须有一个更优雅的方式......

But where?

2 个解决方案

#2


6  

My solution (based on leppie's hint):

我的解决方案(基于leppie的提示):

        Process p = new Process();

        p.StartInfo.FileName = textFilename.Text;
        p.StartInfo.Arguments = textArgument.Text;
        p.StartInfo.UserName = textUsername.Text;
        p.StartInfo.Domain = textDomain.Text;
        p.StartInfo.Password = securePassword.SecureText;

        p.StartInfo.LoadUserProfile = true;
        p.StartInfo.UseShellExecute = false;

        try {
            p.Start();
        } catch (Win32Exception ex) {
            MessageBox.Show("Error:\r\n" + ex.Message);
        }

#1


#2


6  

My solution (based on leppie's hint):

我的解决方案(基于leppie的提示):

        Process p = new Process();

        p.StartInfo.FileName = textFilename.Text;
        p.StartInfo.Arguments = textArgument.Text;
        p.StartInfo.UserName = textUsername.Text;
        p.StartInfo.Domain = textDomain.Text;
        p.StartInfo.Password = securePassword.SecureText;

        p.StartInfo.LoadUserProfile = true;
        p.StartInfo.UseShellExecute = false;

        try {
            p.Start();
        } catch (Win32Exception ex) {
            MessageBox.Show("Error:\r\n" + ex.Message);
        }