-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathProcessStartInfo.cs
More file actions
218 lines (187 loc) · 7.9 KB
/
Copy pathProcessStartInfo.cs
File metadata and controls
218 lines (187 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace System.Diagnostics
{
/// <devdoc>
/// A set of values used to specify a process to start. This is
/// used in conjunction with the <see cref='System.Diagnostics.Process'/>
/// component.
/// </devdoc>
[DebuggerDisplay("FileName = {FileName}, Arguments = {BuildArguments()}, WorkingDirectory = {WorkingDirectory}")]
public sealed partial class ProcessStartInfo
{
private string? _fileName;
private string? _arguments;
private string? _directory;
private string? _userName;
private string? _verb;
private Collection<string>? _argumentList;
private ProcessWindowStyle _windowStyle;
internal DictionaryWrapper? _environmentVariables;
/// <devdoc>
/// Default constructor. At least the <see cref='System.Diagnostics.ProcessStartInfo.FileName'/>
/// property must be set before starting the process.
/// </devdoc>
public ProcessStartInfo()
{
}
/// <devdoc>
/// Specifies the name of the application or document that is to be started.
/// </devdoc>
public ProcessStartInfo(string fileName)
{
_fileName = fileName;
}
/// <devdoc>
/// Specifies the name of the application that is to be started, as well as a set
/// of command line arguments to pass to the application.
/// </devdoc>
public ProcessStartInfo(string fileName, string arguments)
{
_fileName = fileName;
_arguments = arguments;
}
/// <summary>
/// Specifies the name of the application that is to be started, as well as a set
/// of command line arguments to pass to the application.
/// </summary>
public ProcessStartInfo(string fileName, IEnumerable<string> arguments)
{
ArgumentNullException.ThrowIfNull(fileName);
ArgumentNullException.ThrowIfNull(arguments);
_fileName = fileName;
_argumentList = new Collection<string>(new List<string>(arguments));
}
/// <devdoc>
/// Specifies the set of command line arguments to use when starting the application.
/// </devdoc>
[AllowNull]
public string Arguments
{
get => _arguments ?? string.Empty;
set => _arguments = value;
}
public Collection<string> ArgumentList => _argumentList ??= new Collection<string>();
internal bool HasArgumentList => _argumentList is not null && _argumentList.Count != 0;
public bool CreateNoWindow { get; set; }
[Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public StringDictionary EnvironmentVariables => new StringDictionaryWrapper((Environment as DictionaryWrapper)!);
public IDictionary<string, string?> Environment
{
get
{
if (_environmentVariables == null)
{
IDictionary envVars = System.Environment.GetEnvironmentVariables();
_environmentVariables = new DictionaryWrapper(new Dictionary<string, string?>(
envVars.Count,
OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal));
// Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations.
IDictionaryEnumerator e = envVars.GetEnumerator();
Debug.Assert(!(e is IDisposable), "Environment.GetEnvironmentVariables should not be IDisposable.");
while (e.MoveNext())
{
DictionaryEntry entry = e.Entry;
_environmentVariables.Add((string)entry.Key, (string?)entry.Value);
}
}
return _environmentVariables;
}
}
public bool RedirectStandardInput { get; set; }
public bool RedirectStandardOutput { get; set; }
public bool RedirectStandardError { get; set; }
public Encoding? StandardInputEncoding { get; set; }
public Encoding? StandardErrorEncoding { get; set; }
public Encoding? StandardOutputEncoding { get; set; }
/// <devdoc>
/// <para>
/// Returns or sets the application, document, or URL that is to be launched.
/// </para>
/// </devdoc>
[Editor("System.Diagnostics.Design.StartFileNameEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[AllowNull]
public string FileName
{
get => _fileName ?? string.Empty;
set => _fileName = value;
}
/// <devdoc>
/// Returns or sets the initial directory for the process that is started.
/// Specify "" to if the default is desired.
/// </devdoc>
[Editor("System.Diagnostics.Design.WorkingDirectoryEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[AllowNull]
public string WorkingDirectory
{
get => _directory ?? string.Empty;
set => _directory = value;
}
public bool ErrorDialog { get; set; }
public IntPtr ErrorDialogParentHandle { get; set; }
[AllowNull]
public string UserName
{
get => _userName ?? string.Empty;
set => _userName = value;
}
[DefaultValue("")]
[AllowNull]
public string Verb
{
get => _verb ?? string.Empty;
set => _verb = value;
}
[DefaultValueAttribute(System.Diagnostics.ProcessWindowStyle.Normal)]
public ProcessWindowStyle WindowStyle
{
get => _windowStyle;
set
{
if (!Enum.IsDefined(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ProcessWindowStyle));
}
_windowStyle = value;
}
}
internal string BuildArguments()
{
if (HasArgumentList)
{
var arguments = new ValueStringBuilder(stackalloc char[256]);
AppendArgumentsTo(ref arguments);
return arguments.ToString();
}
return Arguments;
}
internal void AppendArgumentsTo(ref ValueStringBuilder stringBuilder)
{
if (_argumentList != null && _argumentList.Count > 0)
{
foreach (string argument in _argumentList)
{
PasteArguments.AppendArgument(ref stringBuilder, argument);
}
}
else if (!string.IsNullOrEmpty(Arguments))
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append(' ');
}
stringBuilder.Append(Arguments);
}
}
}
}