System.Diagnostics.Process可在一台PC上运行,但不适用于另一台PC

时间:2020-01-31 13:18:32

标签: c# cmd visual-studio-2015

我正在使用c#visual studio 2015,我试图以管理员身份更改IP,我具有PC的本地管理员密码,所以我尝试了此代码,它可以在我的PC上正常工作。

问题是,当我从解决方案-> bin中获取.exe文件时,尽管本地管理员密码相同,但试图在另一台PC上使用它却无法正常工作。

    private void button1_Click(object sender, EventArgs e)
    {
    string sysAdminUser = "test1";
    string sysAdminPass = "test2";

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            System.Security.SecureString ssPwd = new System.Security.SecureString();
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.FileName = @"cmd.exe";
            proc.StartInfo.Arguments = "/c netsh interface ip set address \"Ethernet\" static 192.168.1.200 255.255.255.0 192.168.1.1";
            proc.StartInfo.Domain = "";
            proc.StartInfo.Verb = "runas";
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.UserName = sysAdminUser;
            string password = sysAdminPass;
            for (int x = 0; x < password.Length; x++)
            {
                ssPwd.AppendChar(password[x]);
            }
            password = "";
            proc.StartInfo.Password = ssPwd;
            proc.Start();    

    } 

0 个答案:

没有答案