Tuesday, 26 March 2013

System operations in C#


         

System.Diagnostics.Process

The Process class provides functionality to monitor system processes across the network, and to start and stop local system processes.
In additional to retrieving lists of running processes (by specifying either the computer, the process name, or the process ID) or viewing information about the process that currently has access to the processor, you can get detailed knowledge of process threads and modules both through the Process class itself, and by interacting with theProcessThread and ProcessModule classes.
The ProcessStartInfo class enables you to specify a variety of elements with which to start a new process, such as input, output, and error streams, working directories, and command line verbs and arguments. These give you fine control over the behavior of your processes.
Other related classes let you specify window styles, process and thread priorities, and interact with collections of threads and modules.
Some related classes:
  • Process: Provides access to local and remote processes and enables you to start and stop local system processes.
  • ProcessModule: Represents a .dll or .exe file that is loaded into a particular process.
  • ProcessModuleCollection: Provides a strongly typed collection of ProcessModule objects.
  • ProcessStartInfo: Specifies a set of values used when starting a process.
  • ProcessThread: Represents an operating system process thread.
  • ProcessThreadCollection: Provides a strongly typed collection of ProcessThread objects.

Some Operations :


              To ShutDown : shutdown -s

              To Restart : shutdown -r

              To Logoff : shutdown -l

              To Hibernate :%windir%\system32\rundll32.exe PowrProf.dll,SetSuspendState

              To Lock : Rundll32.exe User32.dll,LockWorkStation

              To Sleep : rundll32.exe powrprof.dll,SetSuspendState 0,1,0

            Example : ProcessStartInfo process = new ProcessStartInfo("shutdown.exe", "-s");

Examples:


            ProcessStartInfo process = new ProcessStartInfo("shutdown.exe", "-s");
            Process.Start(process);

            EventLog ev = new EventLog("Application", System.Environment.MachineName, "MyAppName");
            ev.WriteEntry("My event text", System.Diagnostics.EventLogEntryType.Information);
            ev.Close();

            StackTrace stc = new StackTrace(false);
            int framecount = stc.FrameCount;

            StackFrame stFrame = new StackFrame();
            Console.WriteLine("GetFrame method : " + stFrame.GetMethod());
            Console.WriteLine("GetFrame method : " + stFrame.GetMethod().Name.ToString());



            Process proces = new Process();
            proces = Process.Start("notepad.exe");
            proces.WaitForExit(1000);
            proces.CloseMainWindow();   // Closes main interface window

           //To Lock workstation
            Process.Start(@"C:\WINDOWS\system32\rundll32.exe","user32.dll,LockWorkStation");   

No comments:

Post a Comment