Skip to content

Commit

Permalink
add DebuggerDisplay attribute to ProcessStartInfo (#38279)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik authored Jun 26, 2020
1 parent 5e12b4f commit 38f8e38
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ private unsafe bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
if (startInfo._environmentVariables != null)
throw new InvalidOperationException(SR.CantUseEnvVars);

string arguments;
if (startInfo.ArgumentList.Count > 0)
{
StringBuilder sb = new StringBuilder();
Process.AppendArguments(sb, startInfo.ArgumentList);
arguments = sb.ToString();
}
else
{
arguments = startInfo.Arguments;
}
string arguments = startInfo.BuildArguments();

fixed (char* fileName = startInfo.FileName.Length > 0 ? startInfo.FileName : null)
fixed (char* verb = startInfo.Verb.Length > 0 ? startInfo.Verb : null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,7 @@ private unsafe bool StartWithCreateProcess(ProcessStartInfo startInfo)
// * CreateProcess allows you to redirect all or none of the standard IO handles, so we use
// GetStdHandle for the handles that are not being redirected

StringBuilder commandLine = BuildCommandLine(startInfo.FileName, StartInfo.Arguments);
Process.AppendArguments(commandLine, StartInfo.ArgumentList);
StringBuilder commandLine = BuildCommandLine(startInfo);

Interop.Kernel32.STARTUPINFO startupInfo = default;
Interop.Kernel32.PROCESS_INFORMATION processInfo = default;
Expand Down Expand Up @@ -668,16 +667,16 @@ private static Encoding GetEncoding(int codePage)

private bool _signaled;

private static StringBuilder BuildCommandLine(string executableFileName, string arguments)
private static StringBuilder BuildCommandLine(ProcessStartInfo startInfo)
{
// Construct a StringBuilder with the appropriate command line
// to pass to CreateProcess. If the filename isn't already
// in quotes, we quote it here. This prevents some security
// problems (it specifies exactly which part of the string
// is the file to execute).
StringBuilder commandLine = new StringBuilder();
string fileName = executableFileName.Trim();
bool fileNameIsQuoted = (fileName.StartsWith('\"') && fileName.EndsWith('\"'));
ReadOnlySpan<char> fileName = startInfo.FileName.AsSpan().Trim();
bool fileNameIsQuoted = fileName.Length > 0 && fileName[0] == '\"' && fileName[fileName.Length - 1] == '\"';
if (!fileNameIsQuoted)
{
commandLine.Append('"');
Expand All @@ -690,11 +689,7 @@ private static StringBuilder BuildCommandLine(string executableFileName, string
commandLine.Append('"');
}

if (!string.IsNullOrEmpty(arguments))
{
commandLine.Append(' ');
commandLine.Append(arguments);
}
startInfo.AppendArgumentsTo(commandLine);

return commandLine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1610,17 +1610,6 @@ internal void ErrorReadNotifyUser(string? data)
}
}

private static void AppendArguments(StringBuilder stringBuilder, Collection<string> argumentList)
{
if (argumentList.Count > 0)
{
foreach (string argument in argumentList)
{
PasteArguments.AppendArgument(stringBuilder, argument);
}
}
}

/// <summary>
/// This enum defines the operation mode for redirected process stream.
/// We don't support switching between synchronous mode and asynchronous mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace System.Diagnostics
/// 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;
Expand Down Expand Up @@ -167,5 +168,39 @@ public ProcessWindowStyle WindowStyle
_windowStyle = value;
}
}

internal string BuildArguments()
{
if (_argumentList == null || _argumentList.Count == 0)
{
return Arguments;
}
else
{
var stringBuilder = new StringBuilder();
AppendArgumentsTo(stringBuilder);
return stringBuilder.ToString();
}
}

internal void AppendArgumentsTo(StringBuilder stringBuilder)
{
if (_argumentList != null && _argumentList.Count > 0)
{
foreach (string argument in _argumentList)
{
PasteArguments.AppendArgument(stringBuilder, argument);
}
}
else if (!string.IsNullOrEmpty(Arguments))
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append(' ');
}

stringBuilder.Append(Arguments);
}
}
}
}

0 comments on commit 38f8e38

Please sign in to comment.