I have a .NET C# project that compiles against different target frameworks.
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;net6.0-windows</TargetFrameworks>
...
A unit test in VS 2022 (done via Resharper Unit Test sessions) accessing StartInfo of the current process is working fine with 4.6.1, but fails with an exception with net.6.0.
using (Process process = Process.GetCurrentProcess())
{
Assert.IsTrue(process.StartInfo != null);
}
Exception text:
*Test method HSBC.Diagnostics.Test.ProcessProxyTest.TestProcessProxy threw exception:
System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.
at System.Diagnostics.Process.get_StartInfo()
...
What is the reason for it?
I see that for both platforms, the used mainmodule is ...JetBrains\Installations\ReSharperPlatformVs17_95e6680d\TestRunner\net461\ReSharperTestRunner.exe.
Update: this happens also with a quite simple console application:
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
Console.WriteLine("Hello, World!");
using (Process process = Process.GetCurrentProcess())
{
if (process.StartInfo != null)
{
Console.WriteLine("Process.StartInfo found.");
}
}
OK, seems like it was never valid to access StartInfo like that from processes that have not been returned by Start()...
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.startinfo?view=net-6.0
You should only access the StartInfo property on a Process object returned by the Start method. For example, you should not access the StartInfo property on a Process object returned by GetProcesses. Otherwise, on .NET Core the StartInfo property will throw an InvalidOperationException and on .NET Framework it will return a dummy ProcessStartInfo object.