Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageVersion Include="FluentAssertions" Version="8.9.0" />
<PackageVersion Include="Gress" Version="2.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageVersion Include="PolyShim" Version="2.11.0-a.1" />
<PackageVersion Include="PolyShim" Version="2.11.0-a.3" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.5.61" />
Expand Down
19 changes: 12 additions & 7 deletions PowerKit.Tests/Extensions/ProcessExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ namespace PowerKit.Tests.Extensions;

public class ProcessExtensionsTests
{
[Fact]
public void IsRunning_Running_Test()
{
// Act & assert
Process.IsRunning(Environment.ProcessId).Should().BeTrue();
}

[Fact]
public async Task IsRunning_NotRunning_Test()
{
Expand All @@ -29,5 +22,17 @@ public async Task IsRunning_NotRunning_Test()

// Act & assert
Process.IsRunning(processId).Should().BeFalse();
Process.IsRunning(Environment.ProcessId).Should().BeTrue();
}

[Fact]
public async Task Start_Test()
{
// Act
using var process = Process.Start("dotnet", ["--version"]);
await process.WaitForExitAsync();

// Assert
process.ExitCode.Should().Be(0);
}
}
32 changes: 30 additions & 2 deletions PowerKit/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -27,10 +28,37 @@ public static bool IsRunning(int processId)
}
}

/// <summary>
/// Starts a new process using the specified file path and optional arguments.
/// </summary>
public static Process Start(string path, IEnumerable<string>? arguments = null)
{
var process = new Process { StartInfo = new ProcessStartInfo(path) };

if (arguments is not null)
{
foreach (var argument in arguments)
process.StartInfo.ArgumentList.Add(argument);
Comment thread
Tyrrrz marked this conversation as resolved.
}

process.Start();
return process;
}

/// <summary>
/// Starts the process associated with the specified file path or URL, using the operating system shell.
/// </summary>
public static Process? StartShellExecute(string fileName) =>
Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true });
public static Process? StartShellExecute(string path, IEnumerable<string>? arguments = null)
{
var startInfo = new ProcessStartInfo(path) { UseShellExecute = true };

if (arguments is not null)
{
foreach (var argument in arguments)
startInfo.ArgumentList.Add(argument);
}

return Process.Start(startInfo);
}
}
}