Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions PowerKit.Tests/Extensions/ProcessExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public async Task IsRunning_NotRunning_Test()
// Act & assert
Process.IsRunning(processId).Should().BeFalse();
}

[Fact]
public void Start_Test()
{
// Act & assert (should not throw)
Process.Start("dotnet", ["--version"]);
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}

}
43 changes: 43 additions & 0 deletions PowerKit/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
#if NET40_OR_GREATER || NETSTANDARD || NET
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
using System.Collections.Generic;
#endif

namespace PowerKit.Extensions;

Expand All @@ -27,10 +30,50 @@ public static bool IsRunning(int processId)
}
}

#if NET40_OR_GREATER || NETSTANDARD || NET
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
/// <summary>
/// Starts a new process using the specified file path and optional arguments.
/// </summary>
public static void Start(string path, IReadOnlyList<string>? arguments = null)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo(path);

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

process.Start();
}

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

#if NET || NETCOREAPP
if (arguments is not null)
{
foreach (var argument in arguments)
process.StartInfo.ArgumentList.Add(argument);
}
#endif

process.Start();
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
#else
/// <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 });
#endif
}
}