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