Ramitta
我是米塔更新
This commit is contained in:
166
Ramitta/Ramitta.cs
Normal file
166
Ramitta/Ramitta.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Ramitta.lib
|
||||
{
|
||||
public static class Basic
|
||||
{
|
||||
#region 全局颜色
|
||||
public static SolidColorBrush 经典紫色 = new SolidColorBrush(Color.FromRgb(92, 40, 147));
|
||||
public static SolidColorBrush 错误红色 = new SolidColorBrush(Color.FromRgb(134, 27, 45));
|
||||
public static SolidColorBrush 警告橙色 = new SolidColorBrush(Color.FromRgb(202, 81, 0));
|
||||
public static SolidColorBrush 正常绿色 = new SolidColorBrush(Color.FromRgb(66, 164, 60));
|
||||
public static SolidColorBrush 通知蓝色 = new SolidColorBrush(Color.FromRgb(0, 122, 204));
|
||||
public static SolidColorBrush 背景黑色 = new SolidColorBrush(Color.FromRgb(30, 30, 30));
|
||||
public static SolidColorBrush 默认灰色 = new SolidColorBrush(Color.FromRgb(66, 66, 66));
|
||||
#endregion
|
||||
|
||||
#region 调试函数
|
||||
public static void DebugBar(Label obj, String text, SolidColorBrush? color=null)
|
||||
{
|
||||
if (obj == null) return;
|
||||
// 通过Dispatcher确保在UI线程执行
|
||||
obj.Dispatcher.Invoke(() =>
|
||||
{
|
||||
obj.Content = text;
|
||||
if(color!=null)obj.Background = color;
|
||||
});
|
||||
}
|
||||
|
||||
// 定义自定义异常类
|
||||
public class DebugbarException : Exception
|
||||
{
|
||||
public string Message { get; }
|
||||
public SolidColorBrush Brush { get; }
|
||||
|
||||
public DebugbarException(string message, SolidColorBrush brush)
|
||||
: base(message)
|
||||
{
|
||||
Message = message;
|
||||
Brush = brush;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 参数解析
|
||||
public static Dictionary<string, string>? Startupe;
|
||||
public static Dictionary<string, string> ParseCommandLineArgs(string[] args)
|
||||
{
|
||||
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
string arg = args[i];
|
||||
|
||||
// 检查是否是参数名(以-或/开头)
|
||||
if (arg.StartsWith("-") || arg.StartsWith("/"))
|
||||
{
|
||||
string key = arg.TrimStart('-', '/');
|
||||
string value = "true"; // 默认值为true,表示开关存在
|
||||
|
||||
// 检查下一个参数是否是值(不以-或/开头)
|
||||
if (i + 1 < args.Length && !args[i + 1].StartsWith("-") && !args[i + 1].StartsWith("/"))
|
||||
{
|
||||
value = args[i + 1];
|
||||
i++; // 跳过下一个参数,因为已经被用作值
|
||||
}
|
||||
|
||||
arguments[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static async Task<int> RunExternalCommand(string applicationPath, string arguments, bool UseShellExecute = false, bool CreateNoWindow = false)
|
||||
{
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = applicationPath,
|
||||
Arguments = arguments,
|
||||
UseShellExecute = UseShellExecute,
|
||||
CreateNoWindow = CreateNoWindow
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Process process = Process.Start(startInfo) ??
|
||||
throw new InvalidOperationException("Failed to start process.");
|
||||
|
||||
if (UseShellExecute != true)
|
||||
{
|
||||
await Task.Run(() => process.WaitForExit());
|
||||
return process.ExitCode; // 返回进程的退出代码
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> RunExternalCommandResult(string applicationPath, string arguments, bool CreateNoWindow = true)
|
||||
{
|
||||
// 创建一个新的进程启动信息
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = applicationPath,
|
||||
Arguments = arguments,
|
||||
UseShellExecute = false, // 必须设置为 false 以便能够重定向输入输出
|
||||
RedirectStandardOutput = true, // 重定向标准输出
|
||||
RedirectStandardError = true, // 可选:也重定向标准错误流
|
||||
CreateNoWindow = CreateNoWindow // 不创建窗口
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// 启动进程
|
||||
using (Process process = new Process())
|
||||
{
|
||||
process.StartInfo = startInfo;
|
||||
|
||||
// 启动进程
|
||||
process.Start();
|
||||
|
||||
// 读取输出流
|
||||
string output = await process.StandardOutput.ReadToEndAsync();
|
||||
string error = await process.StandardError.ReadToEndAsync(); // 读取标准错误流
|
||||
|
||||
// 等待进程结束
|
||||
await process.WaitForExitAsync();
|
||||
/*
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
// 处理非零退出代码的情况
|
||||
throw new Exception($"Process exited with code {process.ExitCode}: {error}");
|
||||
}
|
||||
*/
|
||||
|
||||
return output; // 返回标准输出内容
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 可以记录或处理异常
|
||||
throw; // 重新抛出异常以便调用者捕获
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user