ProcessStartInfo.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // System.Diagnostics.ProcessStartInfo.cs
  3. //
  4. // Authors:
  5. // Dick Porter ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using Microsoft.Win32;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.IO;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. using System.Text;
  38. namespace System.Diagnostics
  39. {
  40. [TypeConverter (typeof (ExpandableObjectConverter))]
  41. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  42. public sealed class ProcessStartInfo
  43. {
  44. /* keep these fields in this order and in sync with metadata/process.h */
  45. private string arguments = "";
  46. private IntPtr error_dialog_parent_handle = (IntPtr)0;
  47. private string filename = "";
  48. private string verb = "";
  49. private string working_directory = "";
  50. private ProcessStringDictionary envVars;
  51. private bool create_no_window = false;
  52. private bool error_dialog = false;
  53. private bool redirect_standard_error = false;
  54. private bool redirect_standard_input = false;
  55. private bool redirect_standard_output = false;
  56. private bool use_shell_execute = true;
  57. private ProcessWindowStyle window_style = ProcessWindowStyle.Normal;
  58. private Encoding encoding_stderr, encoding_stdout;
  59. private string username, domain;
  60. #if NET_2_0
  61. private SecureString password;
  62. #else
  63. private object password; // dummy
  64. #endif
  65. private bool load_user_profile;
  66. public ProcessStartInfo()
  67. {
  68. }
  69. public ProcessStartInfo(string filename)
  70. {
  71. this.filename = filename;
  72. }
  73. public ProcessStartInfo(string filename, string arguments)
  74. {
  75. this.filename = filename;
  76. this.arguments = arguments;
  77. }
  78. [RecommendedAsConfigurable (true), DefaultValue ("")]
  79. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  80. [MonitoringDescription ("Command line agruments for this process.")]
  81. #if NET_2_0
  82. [NotifyParentPropertyAttribute (true)]
  83. #endif
  84. public string Arguments {
  85. get {
  86. return(arguments);
  87. }
  88. set {
  89. arguments = value;
  90. }
  91. }
  92. [DefaultValue (false)]
  93. [MonitoringDescription ("Start this process with a new window.")]
  94. #if NET_2_0
  95. [NotifyParentPropertyAttribute (true)]
  96. #endif
  97. public bool CreateNoWindow {
  98. get {
  99. return(create_no_window);
  100. }
  101. set {
  102. create_no_window = value;
  103. }
  104. }
  105. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content), DefaultValue (null)]
  106. [Editor ("System.Diagnostics.Design.StringDictionaryEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  107. [MonitoringDescription ("Environment variables used for this process.")]
  108. #if NET_2_0
  109. [NotifyParentPropertyAttribute (true)]
  110. #endif
  111. public StringDictionary EnvironmentVariables {
  112. get {
  113. if (envVars == null) {
  114. envVars = new ProcessStringDictionary ();
  115. foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables ())
  116. envVars.Add ((string) entry.Key, (string) entry.Value);
  117. }
  118. return envVars;
  119. }
  120. }
  121. internal bool HaveEnvVars {
  122. get { return (envVars != null && envVars.Count > 0); }
  123. }
  124. [DefaultValue (false)]
  125. [MonitoringDescription ("Thread shows dialogboxes for errors.")]
  126. #if NET_2_0
  127. [NotifyParentPropertyAttribute (true)]
  128. #endif
  129. public bool ErrorDialog {
  130. get {
  131. return(error_dialog);
  132. }
  133. set {
  134. error_dialog = value;
  135. }
  136. }
  137. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden), Browsable (false)]
  138. public IntPtr ErrorDialogParentHandle {
  139. get {
  140. return(error_dialog_parent_handle);
  141. }
  142. set {
  143. error_dialog_parent_handle = value;
  144. }
  145. }
  146. [RecommendedAsConfigurable (true), DefaultValue ("")]
  147. [Editor ("System.Diagnostics.Design.StartFileNameEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  148. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  149. [MonitoringDescription ("The name of the resource to start this process.")]
  150. #if NET_2_0
  151. [NotifyParentPropertyAttribute (true)]
  152. #endif
  153. public string FileName {
  154. get {
  155. return(filename);
  156. }
  157. set {
  158. filename = value;
  159. }
  160. }
  161. [DefaultValue (false)]
  162. [MonitoringDescription ("Errors of this process are redirected.")]
  163. #if NET_2_0
  164. [NotifyParentPropertyAttribute (true)]
  165. #endif
  166. public bool RedirectStandardError {
  167. get {
  168. return(redirect_standard_error);
  169. }
  170. set {
  171. redirect_standard_error = value;
  172. }
  173. }
  174. [DefaultValue (false)]
  175. [MonitoringDescription ("Standard input of this process is redirected.")]
  176. #if NET_2_0
  177. [NotifyParentPropertyAttribute (true)]
  178. #endif
  179. public bool RedirectStandardInput {
  180. get {
  181. return(redirect_standard_input);
  182. }
  183. set {
  184. redirect_standard_input = value;
  185. }
  186. }
  187. [DefaultValue (false)]
  188. [MonitoringDescription ("Standart output of this process is redirected.")]
  189. #if NET_2_0
  190. [NotifyParentPropertyAttribute (true)]
  191. #endif
  192. public bool RedirectStandardOutput {
  193. get {
  194. return(redirect_standard_output);
  195. }
  196. set {
  197. redirect_standard_output = value;
  198. }
  199. }
  200. #if NET_2_0
  201. public Encoding StandardErrorEncoding {
  202. get { return encoding_stderr; }
  203. set { encoding_stderr = value; }
  204. }
  205. public Encoding StandardOutputEncoding {
  206. get { return encoding_stdout; }
  207. set { encoding_stdout = value; }
  208. }
  209. #endif
  210. [DefaultValue (true)]
  211. [MonitoringDescription ("Use the shell to start this process.")]
  212. #if NET_2_0
  213. [NotifyParentPropertyAttribute (true)]
  214. #endif
  215. public bool UseShellExecute {
  216. get {
  217. return(use_shell_execute);
  218. }
  219. set {
  220. use_shell_execute = value;
  221. }
  222. }
  223. [DefaultValue ("")]
  224. [TypeConverter ("System.Diagnostics.Design.VerbConverter, " + Consts.AssemblySystem_Design)]
  225. [MonitoringDescription ("The verb to apply to a used document.")]
  226. #if NET_2_0
  227. [NotifyParentPropertyAttribute (true)]
  228. #endif
  229. public string Verb {
  230. get {
  231. return(verb);
  232. }
  233. set {
  234. verb = value;
  235. }
  236. }
  237. static readonly string [] empty = new string [0];
  238. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden), Browsable (false)]
  239. public string[] Verbs {
  240. get {
  241. string ext = filename == null | filename.Length == 0 ?
  242. null : Path.GetExtension (filename);
  243. if (ext == null)
  244. return empty;
  245. switch (Environment.OSVersion.Platform) {
  246. case (PlatformID)4:
  247. case (PlatformID)6:
  248. case (PlatformID)128:
  249. return empty; // no verb on non-Windows
  250. default:
  251. RegistryKey rk = null, rk2 = null, rk3 = null;
  252. try {
  253. rk = Registry.ClassesRoot.OpenSubKey (ext);
  254. string k = rk != null ? rk.GetValue (null) as string : null;
  255. rk2 = k != null ? Registry.ClassesRoot.OpenSubKey (k) : null;
  256. rk3 = rk2 != null ? rk2.OpenSubKey ("shell") : null;
  257. return rk3 != null ? rk3.GetSubKeyNames () : null;
  258. } finally {
  259. if (rk3 != null)
  260. rk3.Close ();
  261. if (rk2 != null)
  262. rk2.Close ();
  263. if (rk != null)
  264. rk.Close ();
  265. }
  266. }
  267. }
  268. }
  269. [DefaultValue (typeof (ProcessWindowStyle), "Normal")]
  270. [MonitoringDescription ("The window style used to start this process.")]
  271. #if NET_2_0
  272. [NotifyParentPropertyAttribute (true)]
  273. #endif
  274. public ProcessWindowStyle WindowStyle {
  275. get {
  276. return(window_style);
  277. }
  278. set {
  279. window_style = value;
  280. }
  281. }
  282. [RecommendedAsConfigurable (true), DefaultValue ("")]
  283. [Editor ("System.Diagnostics.Design.WorkingDirectoryEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  284. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  285. [MonitoringDescription ("The initial directory for this process.")]
  286. #if NET_2_0
  287. [NotifyParentPropertyAttribute (true)]
  288. #endif
  289. public string WorkingDirectory {
  290. get {
  291. return(working_directory);
  292. }
  293. set {
  294. working_directory = value == null ? String.Empty : value;
  295. }
  296. }
  297. #if NET_2_0
  298. [NotifyParentPropertyAttribute (true)]
  299. public bool LoadUserProfile {
  300. get { return load_user_profile; }
  301. set { load_user_profile = value; }
  302. }
  303. [NotifyParentPropertyAttribute (true)]
  304. public string UserName {
  305. get { return username; }
  306. set { username = value; }
  307. }
  308. [NotifyParentPropertyAttribute (true)]
  309. public string Domain {
  310. get { return domain; }
  311. set { domain = value; }
  312. }
  313. public SecureString Password {
  314. get { return password; }
  315. set { password = value; }
  316. }
  317. #endif
  318. }
  319. }