Csharp/CSharp Tutorial/Windows/Windows Application Process

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

IE processes

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.rupilerServices;
using System.Security;
using System.Text;
using System.Threading;
public class MainClass
{
    public static void Main()
    {
        Process[] iexplorerProcs = Process.GetProcessesByName("iexplore");
        foreach (Process p in iexplorerProcs) 
           Console.WriteLine("  -> {0} - {1}", p.ProcessName, p.PeakWorkingSet64);
    }
}

Start Notepad as the specified user

using System;
using System.Security;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        Console.Write("Enter the user"s password: ");
        SecureString str = new SecureString();
        str.AppendChar("A");
        str.AppendChar("B");
        str.AppendChar("C");
        str.MakeReadOnly();
        
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.UserName = "user";
        //startInfo.Password = "pword";
        startInfo.UseShellExecute = false;
        using (Process process = new Process())
        {
            process.StartInfo = startInfo;
            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\nCould not start Notepad process.");
                Console.WriteLine(ex);
            }
        }
    }
}

Terminating Notepad with CloseMainWindow

using System;
using System.Threading;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        using (Process process = Process.Start("notepad.exe",@"c:\test.txt"))
        {
            Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
            Thread.Sleep(5000);
            if (!process.CloseMainWindow())
            {
                Console.WriteLine("CloseMainWindow returned false - terminating Notepad with Kill.");
                process.Kill();
            }
            else
            {
                if (!process.WaitForExit(2000))
                {
                    Console.WriteLine("CloseMainWindow failed to terminate - terminating Notepad with Kill.");
                    process.Kill();
                }
            }
        }
    }
}

Terminating Notepad with Kill

using System;
using System.Threading;
using System.Diagnostics;
class MainClass
{
    public static void Main()
    {
        using (Process process = Process.Start("notepad.exe",@"c:\test.txt"))
        {
            Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
            Thread.Sleep(5000);
            if (!process.CloseMainWindow())
            {
                Console.WriteLine("CloseMainWindow returned false - terminating Notepad with Kill.");
                process.Kill();
            }
            else
            {
                if (!process.WaitForExit(2000))
                {
                    Console.WriteLine("CloseMainWindow failed to terminate - terminating Notepad with Kill.");
                    process.Kill();
                }
            }
        }
    }
}