From 1bfb88010f0495b59d76f02ecd34214bb09afa27 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Sun, 29 Sep 2024 13:37:12 +0200 Subject: [PATCH] fix doc --- docs/Rules/MA0161.md | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/Rules/MA0161.md b/docs/Rules/MA0161.md index faccc863c..d344d7e81 100644 --- a/docs/Rules/MA0161.md +++ b/docs/Rules/MA0161.md @@ -1 +1,43 @@ -# MA0161 - UseShellExecute must be explicitly set \ No newline at end of file +# MA0161 - UseShellExecute must be explicitly set + +Detects when `Process.Start` is called without specifying the value of `UseShellExecute`. + +Specifying the value is important because: +- The default value for this property is `true` on .NET Framework apps and `false` on .NET Core apps. It's a common issue when migrating a desktop app from .NET Framework to .NET Core. +- It must be set to to `false` when redirecting I/O. Otherwise you'll get an issue at runtime. + + +````c# +using System.Diasgnostics; + +// Non compliant + +Process.Start("cmd"); // Intent is not clear if you want to use ShellExecute or not + +Process.Start("https://www.meziantou.net/"); // Will fail on .NET Core apps + +Process.Start(new ProcessStartInfo("cmd") +{ + RedirectStandardOutput = true, + UseShellExecute = true, +}); // It will throw with error "UseShellExecute must be set to false when redirecting I/O" + +Process.Start(new ProcessStartInfo("cmd") +{ + RedirectStandardOutput = true, +}); // It will throw with error "UseShellExecute must be set to false when redirecting I/O" on .NET Framework apps + +// Compliant + +Process.Start(new ProcessStartInfo("https://www.meziantou.net/") +{ + UseShellExecute = true, +}); + +Process.Start(new ProcessStartInfo("cmd") +{ + RedirectStandardOutput = true, + UseShellExecute = false, +}); + +```` \ No newline at end of file