System.Diagnostics.Process.Start(ProcessStartInfo)
Process.Start(ProcessStartInfo)
Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.
public static System.Diagnostics.Process Start (System.Diagnostics.ProcessStartInfo startInfo);
Parameters
- startInfo
- ProcessStartInfo
The ProcessStartInfo that contains the information that is used to start the process, including the file name and any command-line arguments.
Returns
A new Process that is associated with the process resource, or null
if no process resource is started. Note that a new process that's started alongside already running instances of the same process will be independent from the others.
In addition, Start may return a non-null Process with its HasExited property already set to true
. In this case, the started process may have activated an existing instance of itself and then exited.
Exceptions
No file name was specified in the startInfo
parameter's FileName property.
-or-
The UseShellExecute property of the startInfo
parameter is true
and the RedirectStandardInput, RedirectStandardOutput, or RedirectStandardError property is also true
.
-or-
The UseShellExecute property of the startInfo
parameter is true
and the UserName property is not null
or empty or the Password property is not null
.
The startInfo
parameter is null
.
The process object has already been disposed.
An error occurred when opening the associated file.
-or-
The file specified in the startInfo
parameter's FileName property could not be found.
-or-
The sum of the length of the arguments and the length of the full path to the process exceeds 2080. The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." or "Access is denied."
Method not supported on operating systems without shell support such as Nano Server (.NET Core only).
Remarks
Use this overload to start a process resource by specifying a ProcessStartInfo instance. The overload associates the resource with a new Process object.
Note
If the address of the executable file to start is a URL, the process is not started and null
is returned.
This overload lets you start a process without first creating a new Process instance. Using this overload with a ProcessStartInfo parameter is an alternative to the explicit steps of creating a new Process instance, setting its StartInfo properties, and calling Start for the Process instance.
Using a ProcessStartInfo instance as the parameter lets you call Start with the most control over what is passed into the call to start the process. If you need to pass only a file name or a file name and arguments, it is not necessary to create a new ProcessStartInfo instance, although that is an option. The only Process.StartInfo property that must be set is the FileName property. The FileName property does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application that is installed on the system. For example, the FileName property can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc extension if you have associated .doc files with a word processing tool, such as Microsoft Word.
You can start a ClickOnce application by specifying the location (for example, a Web address) from which you originally installed the application. Do not start a ClickOnce application by specifying its installed location on your hard drive.
If the ProcessStartInfo.UserName and ProcessStartInfo.Password properties of the StartInfo instance are set, the unmanaged CreateProcessWithLogonW
function is called, which starts the process in a new window even if the ProcessStartInfo.CreateNoWindow property value is true
or the ProcessStartInfo.WindowStyle property value is ProcessWindowStyle.Hidden. If the ProcessStartInfo.Domain property is null
, the ProcessStartInfo.UserName property must be in UPN format, user@DNS_domain_name.
Unlike the other overloads, the overload of Start that has no parameters is not a static
member. Use that overload when you have already created a Process instance and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. Use one of the static
overloads when you want to create a new Process component rather than start a process for an existing component. Both this overload and the overload that has no parameters allow you to specify the start information for the process resource by using a ProcessStartInfo instance.
If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. Otherwise, the system will not find the path. For example, if c:\mypath
is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath"
, you must fully qualify any process in c:\mypath
when starting it.
Note
ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. The process does not start in the same context as the client browser, and does not have access to the user desktop.
Whenever you use Start to start a process, you might need to close it or you risk losing system resources. Close processes using CloseMainWindow or Kill. You can check whether a process has already been closed by using its HasExited property.
A note about apartment states in managed threads is necessary here. When UseShellExecute is true
on the startInfo
parameter, make sure you have set a threading model on your application by setting the attribute [STAThread]
on the main()
method. Otherwise, a managed thread can be in an unknown
state or put in the MTA
state, the latter of which conflicts with UseShellExecute being true
. Some methods require that the apartment state not be unknown
. If the state is not explicitly set, when the application encounters such a method, it defaults to MTA
, and once set, the apartment state cannot be changed. However, MTA
causes an exception to be thrown when the operating system shell is managing the thread.
Why is my process's Exited method not being called?
In order to receive a callback on Exited
event, the EnableRaisingEvents
must be set to true.
Process correctionProcess = Process.Start(startInfo);
correctionProcess.EnableRaisingEvents = true;
correctionProcess.Exited += new EventHandler(ProcessExited);
private void Process_Exited(object sender, EventArgs e) { LogUtil.CreateLog(LogLevel.Message, "Process_Exited"); if (sender is Process myProcess) { LogUtil.CreateLog(LogLevel.Message, $"Exit time : {myProcess.ExitTime}\n" + $"Exit code : {myProcess.ExitCode}\n" + $"Elapsed time : {Math.Round((myProcess.ExitTime - myProcess.StartTime).TotalMilliseconds)}"); } }
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-07-01 jQuery find
2019-07-01 jQuery file upload里面的_create的调用和_initEventHandlers的调用
2019-07-01 How jQuery UI Works
2019-07-01 How to Create a Basic Plugin 如何写一个基础的jQuery插件
2019-07-01 npm-package-lock.json
2019-07-01 jQuery widget ui里面比较重要的函数
2019-07-01 jQuery file upload cropper的 click .preview事件没有绑定成功