【问题标题】:Execute another EXE from an application with parameters, as admin以管理员身份从带有参数的应用程序执行另一个 EXE
【发布时间】:2016-06-02 12:53:58
【问题描述】:

我在同一个解决方案下创建了两个项目。 ProjectA 是一个 Windows 窗体应用程序,ProjectB 是一个简单的控制台应用程序。ProjectB 将以管理员权限从 ProjectA 执行。
来自 ProjectA 的示例

private void btnFinish_Click(object sender, EventArgs e)
        {
            ipAddress = txtIP.Text;
            bindingPort = txtPort.Text;
            if (!fileChosen)
            {
                CreateCertificate();
                //
            }
            //After this step i want to execute ProjectB with admin provileges with 3 parameters
            ExecuteB_AsAdminWithPrivileges(ipAddress, bindingPort, serverCert);
        }
    }

所以当我单击按钮名称完成时,我希望 ProjectB.exe 使用我将从 ProjectA 提供的参数执行。
ProjectB 看起来像:

public static void StoreAndBindCertificate(string pfxFileServerCert, string ipAddress, string ipPort)
        {
//
}

这是将使用 ProjectA 中的参数的方法。
如何将 ProjectA 中的参数获取到 ProjectB 中的此方法?

【问题讨论】:

    标签: c# .net parameter-passing exe elevated-privileges


    【解决方案1】:

    你可以使用这个方法:

    public static int RunProcessAsAdmin(string exeName, string parameters)
    {
        try {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = CurrentDirectory;
            startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
            startInfo.Verb = "runas";
            //MLHIDE
            startInfo.Arguments = parameters;
            startInfo.ErrorDialog = true;
    
            Process process = System.Diagnostics.Process.Start(startInfo);
            process.WaitForExit();
            return process.ExitCode;
        } catch (Win32Exception ex) {
            WriteLog(ex);
            switch (ex.NativeErrorCode) {
                case 1223:
                    return ex.NativeErrorCode;
                default:
                    return ErrorReturnInteger;
            }
    
        } catch (Exception ex) {
            WriteLog(ex);
            return ErrorReturnInteger;
        }
    }
    

    第一个参数是您的.exe 文件,第二个参数是您要提供给.exe 文件的参数 在此之后,您应该在 main 部分中的 .exe 文件中进行更改。 比如:

    static void Main(string[] args)
            {
                if (args.Length <= 1) return;
    
                try
                {
                    if (args.Length == 2)
                    {
                        _IpAddress = args[0];
                        _IpPort = args[1];
                        FunctionName(_IpAddress, _IpPort);
                    }
                    else
                    {
                        _return
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Invalid number of parameters!");
                }
            }
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      更新

      ProgramA{
      string ip ="123.123.123";
      File.WriteAllText("c://MtDataFromA.txt","ip="+ip);
      }
      
      
      private void btnFinish_Click(object sender, EventArgs e)
                  {
                      ipAddress = File.WriteAllText("c://MtDataFromA.txt");//some algorithem to find the ip from text file
      
          }
      
      
      public static void StoreAndBindCertificate(string pfxFileServerCert, string ipAddress, string ipPort){
      
      
              // Use ProcessStartInfo class
              ProcessStartInfo startInfo = new ProcessStartInfo();
              startInfo.CreateNoWindow = false;
              startInfo.UseShellExecute = false;
              startInfo.FileName = "YourFile.exe";
              startInfo.WindowStyle = ProcessWindowStyle.Hidden;
              startInfo.Arguments = "ipAddress"+" " +"ipPort";
      
              try
              {
                  // Start the process with the info we specified.
                  // Call WaitForExit and then the using statement will close.
                  using (Process exeProcess = Process.Start(startInfo))
                  {
                      exeProcess.WaitForExit();
                  }
              }
      catch
              {
                   // Log error.
              }
      }
      

      link

      【讨论】:

      • 在这种情况下意味着我在这里已经有了 ipAddress 和 ipPort 参数。我的问题是如何从另一个项目中获取这两个参数并在这里使用它们?
      • 此方法 StoreAndBindCertificate 属于 ProjectB 命名空间。所以,我想从 ProjectA 以管理员身份执行这个 ProjectB.exe
      • 好的,您可以在 ProjectA 运行时将 ipPort 和 ipAdress 等数据存储在某个文件夹的某个文本文件中,然后从您在此函数中创建的文件中读取它。
      • 感谢您的帮助
      猜你喜欢
      • 2013-05-24
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2013-05-31
      • 2019-03-25
      • 2011-09-24
      • 2010-12-19
      相关资源
      最近更新 更多