在C#编程中,ProcessStartInfo是一个类,用于包含有关要启动的进程的信息。 它用于Process类的Start方法,以确定如何启动新的进程。
ProcessStartInfo类的一些重要属性包括:
-
FileName:要运行的文件的名称,可含路径(绝对路径或相对路径)。 -
Arguments:传递给文件的命令行参数。 -
WorkingDirectory:工作目录的路径。 -
UseShellExecute:一个布尔值,确定是否使用操作系统外壳程序(shell)启动进程。 -
RedirectStandardInput,RedirectStandardOutput,RedirectStandardError:布尔值,确定是否重定向进程的标准输入、输出和错误流。 -
StandardOutputEncoding,StandardErrorEncoding:用于读取进程的标准输出和错误的编码。 -
CreateNoWindow:一个布尔值,确定是否在新窗口中启动进程。 -
WindowStyle:窗口样式(最大化、最小化、隐藏等)。 -
LoadUserProfile:一个布尔值,确定是否加载用户的配置文件。 -
EnvironmentVariables:一个字符串字典,用于设置环境变量。 -
UserName,Password,Domain:用于身份验证的凭据。
一、简单示例:
string arg1 = "a", arg2 = "b";
string args = $@"{arg1} {arg2}";
ProcessStartInfo info = new ProcessStartInfo();
//指定exe文件
info.FileName = "D:\Test\Test.exe";
//传入参数,多个参数用英文空格隔开
info.Arguments = args;
//已管理员身份运行,可消除打开的确认弹窗
info.Verb = "runas";
//设置不在新窗口中启动新的进程
info.CreateNoWindow = true;
//不使用操作系统使用的shell启动进程
info.UseShellExecute = false;
//将输出信息重定向
info.RedirectStandardOutput = true;
Process pro = Process.Start(info);
关于参数Arguments的说明:传入的是字符串类型,如果需要传入多个参数,请用英文空格隔开。同时被打开的exe程序需要修改Program.cs文件中的应用程序的入口点Main函数,用于接收传入的参数,这个过程会自动解析将参数用英文空格分隔成string数组,如下:
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//调用WinForm的窗体有参构造函数
Application.Run(new TestFrom(args));
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="args">ProcessStartInfo传入的参数</param>
public TestFrom(string[] args)
{
InitializeComponent();
}
二、使用指定的域账号打开exe程序
UserName:用户域账号;
Password:用户域账号的密码;
Domain:域名。
需要用到ShareTool共享工具类:用来登录
using System;
using System.Runtime.InteropServices;
namespace ShareToolClass
{
public class ShareTool : IDisposable
{
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static bool CloseHandle(IntPtr handle);
[DllImport("Advapi32.DLL")]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("Advapi32.DLL")]
static extern bool RevertToSelf();
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NEWCREDENTIALS = 9;
const int LOGON32_LOGON_INTERACTIVE = 2;
private bool disposed;
public ShareTool(string username, string password, string ip)
{
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
try
{
// get handle to token
bool bImpersonated = LogonUser(username, ip, password,
LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);
if (bImpersonated)
{
if (!ImpersonateLoggedOnUser(pExistingTokenHandle))
{
int nErrorCode = Marshal.GetLastWin32Error();
throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);
}
}
else
{
int nErrorCode = Marshal.GetLastWin32Error();
throw new Exception("LogonUser error;Code=" + nErrorCode);
}
}
finally
{
// close handle(s)
if (pExistingTokenHandle != IntPtr.Zero)
CloseHandle(pExistingTokenHandle);
if (pDuplicateTokenHandle != IntPtr.Zero)
CloseHandle(pDuplicateTokenHandle);
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
RevertToSelf();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
}
示例:文章来源:https://www.uudwc.com/A/PmvgW/
string user = "admin";
string userpsw = "123456";
string domain = "TestDomain";
using (ShareToolClass.ShareTool tool = new ShareTool.ShareTool(user, userpsw, domain))
{
//保密文本
SecureString pwd = new SecureString();
foreach (char c in userpsw)
pwd.AppendChar(c);
//使用线程打开
Thread t = new Thread(new ThreadStart(delegate
{
try
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "D:\Test\Test.exe";
info.UserName = user;//域账号
info.Password = pwd;//密码
info.Domain = domain;//域名
//设置不在新窗口中启动新的进程
info.CreateNoWindow = true;
//不使用操作系统使用的shell启动进程
info.UseShellExecute = false;
//将输出信息重定向
//info.RedirectStandardOutput = true;
Process pro = Process.Start(info);
}
catch (Exception ex)
{
PubLibrary.WriteErrLog("打开出错:" + ex.ToString());
}
}));
t.Start();
}
文章来源地址https://www.uudwc.com/A/PmvgW/