Csharp/CSharp Tutorial/Development/ProcessStartInfo

Материал из .Net Framework эксперт
Версия от 12:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create ProcessStartInfo

using System;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;
        Process process;
        try
        {
            process = Process.Start(startInfo);
            Console.WriteLine("Waiting 30 seconds for process to finish.");
            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}
Waiting 30 seconds for process to finish.
Process terminated.

Process Window Style: Maximized

using System;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;
        Process process;
        try
        {
            process = Process.Start(startInfo);
            Console.WriteLine("Waiting 30 seconds for process to finish.");
            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}
Waiting 30 seconds for process to finish.
Process terminated.

Start a Process by using ProcessStartInfo

using System;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;
        Process process;
        try
        {
            process = Process.Start(startInfo);
            Console.WriteLine("Waiting 30 seconds for process to finish.");
            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}
Waiting 30 seconds for process to finish.
Process terminated.

Wait a Process For Exit

using System;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;
        Process process;
        try
        {
            process = Process.Start(startInfo);
            Console.WriteLine("Waiting 30 seconds for process to finish.");
            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}
Waiting 30 seconds for process to finish.
Process terminated.