Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Process.Start (String)

 

Publicado: octubre de 2016

Inicia un recurso de proceso con la especificación del nombre de un documento o de un archivo de aplicación y asocia el recurso a un nuevo componente Process.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

public static Process Start(
	string fileName
)

Parámetros

fileName
Type: System.String

Nombre de un archivo de documento o aplicación que se va a ejecutar en el proceso.

Valor devuelto

Type: System.Diagnostics.Process

Nuevo Process que está asociado al recurso del proceso, o null si no se inicia ningún recurso de proceso. Tenga en cuenta que un proceso nuevo que se inicie junto a instancias del mismo proceso ya en ejecución será independiente del resto. Además, Inicio puede devolver un proceso no nulo con su propiedad HasExited ya establecida en true. En tal caso, el proceso iniciado puede haber activado una instancia existente de sí mismo y, a continuación, haber salido.

Exception Condition
Win32Exception

Se produjo un error al abrir el archivo asociado.

ObjectDisposedException

El objeto del proceso ya se ha desechado.

FileNotFoundException

La variable de entorno PATH tiene una cadena que contiene comillas.

Use this overload to start a process resource by specifying its file name. The overload associates the resource with a new T:System.Diagnostics.Process object.

System_CAPS_noteNota

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 T:System.Diagnostics.Process instance. The overload is an alternative to the explicit steps of creating a new T:System.Diagnostics.Process instance, setting the P:System.Diagnostics.ProcessStartInfo.FileName member of the P:System.Diagnostics.Process.StartInfo property, and calling M:System.Diagnostics.Process.Start for the T:System.Diagnostics.Process instance.

You can start a ClickOnce application by setting the fileName parameter to 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.

Starting a process by specifying its file name is similar to typing the information in the Run dialog box of the Windows Start menu. Therefore, the file name 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 installed on the system. For example the file name can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc if you have associated.doc files with a word processing tool, such as Microsoft Word. Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad".

This overload does not allow command-line arguments for the process. If you need to specify one or more command-line arguments for the process, use the M:System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo) or M:System.Diagnostics.Process.Start(System.String,System.String) overloads.

Unlike the other overloads, the overload of M:System.Diagnostics.Process.Start that has no parameters is not a static member. Use that overload when you have already created a T:System.Diagnostics.Process instance, specified start information (including the file name), and want to start a process resource and associate it with the existing T:System.Diagnostics.Process instance. Use one of the static overloads when you want to create a new T:System.Diagnostics.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 file name of the process resource to start.

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.

System_CAPS_noteNota

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 Overload:System.Diagnostics.Process.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 M:System.Diagnostics.Process.Start to start a process, you might need to close it or you risk losing system resources. Close processes using M:System.Diagnostics.Process.CloseMainWindow or M:System.Diagnostics.Process.Kill. You can check whether a process has already been closed by using its P:System.Diagnostics.Process.HasExited property.

A note about apartment states in managed threads is necessary here. When P:System.Diagnostics.ProcessStartInfo.UseShellExecute is true on the process component's P:System.Diagnostics.Process.StartInfo property, 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 P:System.Diagnostics.ProcessStartInfo.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.

The following example first spawns an instance of Internet Explorer and displays the contents of the Favorites folder in the browser. It then starts some other instances of Internet Explorer and displays some specific pages or sites. Finally it starts Internet Explorer with the window being minimized while navigating to a specific site.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        // Opens the Internet Explorer application.
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");

            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
        }

        // Opens urls and .html documents using Internet Explorer.
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }

        // Uses the ProcessStartInfo class to start new processes,
        // both in a minimized mode.
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);

            startInfo.Arguments = "www.northwindtraders.com";

            Process.Start(startInfo);
        }

        static void Main()
        {
            // Get the path that stores favorite links.
            string myFavoritesPath =
                Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            MyProcess myProcess = new MyProcess();

            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();
        }
    }
}

LinkDemand

for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: