├── .gitattributes ├── .gitignore ├── ProcessUtil.sln ├── ProcessUtil ├── Options.cs ├── Param.cs ├── ProcessExtension.cs ├── ProcessUtil.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── SuspendFlags.cs └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /ProcessUtil.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessUtil", "ProcessUtil\ProcessUtil.csproj", "{9008E9FE-464A-42A4-88C7-C2664DC6B341}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Debug|Mixed Platforms = Debug|Mixed Platforms 10 | Debug|x86 = Debug|x86 11 | Release|Any CPU = Release|Any CPU 12 | Release|Mixed Platforms = Release|Mixed Platforms 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Debug|Any CPU.ActiveCfg = Debug|x86 17 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 18 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Debug|Mixed Platforms.Build.0 = Debug|x86 19 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Debug|x86.ActiveCfg = Debug|x86 20 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Debug|x86.Build.0 = Debug|x86 21 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Release|Any CPU.ActiveCfg = Release|x86 22 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Release|Mixed Platforms.ActiveCfg = Release|x86 23 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Release|Mixed Platforms.Build.0 = Release|x86 24 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Release|x86.ActiveCfg = Release|x86 25 | {9008E9FE-464A-42A4-88C7-C2664DC6B341}.Release|x86.Build.0 = Release|x86 26 | EndGlobalSection 27 | GlobalSection(SolutionProperties) = preSolution 28 | HideSolutionNode = FALSE 29 | EndGlobalSection 30 | EndGlobal 31 | -------------------------------------------------------------------------------- /ProcessUtil/Options.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ProcessUtil 7 | { 8 | public enum Options 9 | { 10 | List, 11 | Kill, 12 | Suspend, 13 | Resume 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ProcessUtil/Param.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ProcessUtil 7 | { 8 | public class Param 9 | { 10 | public int PId { get; set; } 11 | public string Expression { get; set; } 12 | public Options Option { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ProcessUtil/ProcessExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Runtime.InteropServices; 4 | 5 | //Code source from 6 | //http://stackoverflow.com/questions/71257/suspend-process-in-c-sharp 7 | 8 | public static class ProcessExtension 9 | { 10 | [DllImport("kernel32.dll")] 11 | static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId); 12 | [DllImport("kernel32.dll")] 13 | static extern uint SuspendThread(IntPtr hThread); 14 | [DllImport("kernel32.dll")] 15 | static extern int ResumeThread(IntPtr hThread); 16 | 17 | public static void Suspend(this Process process) 18 | { 19 | foreach (ProcessThread thread in process.Threads) 20 | { 21 | var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); 22 | if (pOpenThread == IntPtr.Zero) 23 | { 24 | break; 25 | } 26 | SuspendThread(pOpenThread); 27 | } 28 | } 29 | public static void Resume(this Process process) 30 | { 31 | foreach (ProcessThread thread in process.Threads) 32 | { 33 | var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); 34 | if (pOpenThread == IntPtr.Zero) 35 | { 36 | break; 37 | } 38 | ResumeThread(pOpenThread); 39 | } 40 | } 41 | public static void Print(this Process process) 42 | { 43 | Console.WriteLine("{0,8} {1}", process.Id, process.ProcessName); 44 | } 45 | } -------------------------------------------------------------------------------- /ProcessUtil/ProcessUtil.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {9008E9FE-464A-42A4-88C7-C2664DC6B341} 9 | Exe 10 | Properties 11 | ProcessUtil 12 | ps 13 | v4.0 14 | Client 15 | 512 16 | publish\ 17 | true 18 | Disk 19 | false 20 | Foreground 21 | 7 22 | Days 23 | false 24 | false 25 | true 26 | 0 27 | 1.0.0.%2a 28 | false 29 | false 30 | true 31 | 32 | 33 | x86 34 | true 35 | full 36 | false 37 | ..\..\..\Oneup\Global\Exes\ 38 | DEBUG;TRACE 39 | prompt 40 | 4 41 | 42 | 43 | x86 44 | pdbonly 45 | true 46 | bin\Release\ 47 | TRACE 48 | prompt 49 | 4 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | False 71 | Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 72 | true 73 | 74 | 75 | False 76 | .NET Framework 3.5 SP1 Client Profile 77 | false 78 | 79 | 80 | False 81 | .NET Framework 3.5 SP1 82 | false 83 | 84 | 85 | False 86 | Windows Installer 3.1 87 | true 88 | 89 | 90 | 91 | 98 | -------------------------------------------------------------------------------- /ProcessUtil/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics; 6 | 7 | namespace ProcessUtil 8 | { 9 | class Program 10 | { 11 | 12 | static string argumentMessage = "Usage ps -option[l for list,k for kill,-s for suspend, -r for resume] -regex[Expression for the opertion of the process id]"; 13 | static void Main(string[] args) 14 | { 15 | try{ 16 | var param = ProcessArgs(args); 17 | ExecuteOption(param); 18 | } 19 | catch(ArgumentException e) 20 | { 21 | Console.WriteLine(e.Message); 22 | } 23 | } 24 | 25 | private static void ExecuteOption(Param param) 26 | { 27 | if (param.PId != 0) 28 | { 29 | var process = Process.GetProcessById(param.PId); 30 | ExecuteOption(param, process); 31 | return; 32 | } 33 | if (param.Expression != null) 34 | { 35 | var list = Process.GetProcesses().Where(x => x.ProcessName.IndexOf(param.Expression,StringComparison.OrdinalIgnoreCase)>=0); 36 | foreach (var process in list) 37 | ExecuteOption(param, process); 38 | return; 39 | } 40 | 41 | } 42 | 43 | private static void ExecuteOption(Param param, Process process) 44 | { 45 | Options option = param.Option; 46 | switch (option) 47 | { 48 | case Options.List: 49 | process.Print(); 50 | break; 51 | case Options.Kill: 52 | process.Kill(); 53 | break; 54 | case Options.Suspend: 55 | process.Suspend(); 56 | break; 57 | case Options.Resume: 58 | process.Resume(); 59 | break; 60 | default: 61 | throw new ArgumentException(argumentMessage); 62 | } 63 | } 64 | 65 | public static Param ProcessArgs(string[] args) 66 | { 67 | Param param; 68 | string commandLineParam2; 69 | if(args.Length < 1) 70 | throw new ArgumentException(argumentMessage); 71 | var option = ProcessOption(args[0]); 72 | if(option != Options.List && args.Length < 2) 73 | throw new ArgumentException(argumentMessage); 74 | if (args.Length < 2) 75 | commandLineParam2 = string.Empty; 76 | else 77 | commandLineParam2 = args[1]; 78 | param = ProcessParam(commandLineParam2); 79 | param.Option =option; 80 | return param; 81 | } 82 | public static Param ProcessParam(string rawParam) 83 | { 84 | int result; 85 | var param = new Param(); 86 | if (int.TryParse(rawParam, out result)) 87 | { 88 | param.PId = result; 89 | } 90 | else 91 | { 92 | param.PId = 0; 93 | param.Expression = rawParam; 94 | } 95 | return param; 96 | } 97 | public static Options ProcessOption(string option) 98 | { 99 | Options progOptions; 100 | switch(option) 101 | { 102 | case "-l": 103 | progOptions = Options.List; 104 | break; 105 | case "-k": 106 | progOptions = Options.Kill; 107 | break; 108 | case "-s": 109 | progOptions = Options.Suspend; 110 | break; 111 | case "-r": 112 | progOptions = Options.Resume; 113 | break; 114 | default: 115 | throw new ArgumentException("Param 1 can be -l for list and -k for kill -s for suspend and -r for resume"); 116 | } 117 | return progOptions; 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /ProcessUtil/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ProcessUtil")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ProcessUtil")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f7083764-ec45-4127-b789-5bd386ad3c9e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ProcessUtil/SuspendFlags.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | [Flags] 3 | public enum ThreadAccess : int 4 | { 5 | TERMINATE = (0x0001), 6 | SUSPEND_RESUME = (0x0002), 7 | GET_CONTEXT = (0x0008), 8 | SET_CONTEXT = (0x0010), 9 | SET_INFORMATION = (0x0020), 10 | QUERY_INFORMATION = (0x0040), 11 | SET_THREAD_TOKEN = (0x0080), 12 | IMPERSONATE = (0x0100), 13 | DIRECT_IMPERSONATION = (0x0200) 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ProcessUtil 2 | =========== 3 | 4 | List, Kill, Suspend or Resume a process from command line. 5 | 6 | Eg 7 | 8 | ps -l devenv [List the processes called devenv]
9 | ps -k devenv [Kill all the processes called devenv]
10 | ps -s devenv [Suspend all the processes called devenv]
11 | ps -r devenv [Resume all the processes called devenv]
12 | --------------------------------------------------------------------------------