This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Cmd.cs
102 lines (86 loc) · 2.96 KB
/
Cmd.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using XJK;
using XJK.SysX.CommandHelper;
using static XJK.SysX.CommandHelper.CommandLineHelper;
namespace XJK.SysX
{
public static class Cmd
{
public static string GetRunVerbs(string filename)
{
return (new ProcessStartInfo(filename)).Verbs.JoinToString(", ");
}
public static bool IsUrl(string str)
{
string t = str.ToLower();
return t.StartsWith("http://") || t.StartsWith("https://");
}
public static void Explorer(string path)
{
if (File.Exists(path))
{
RunAsInvoker("explorer", "/select, " + path);
}
else if (Directory.Exists(path))
{
RunAsInvoker("explorer", path);
}
else
{
RunSmart("explorer");
}
}
public static (string, string) SplitCommandArg(string CommandWithArgs)
{
var argArray = CommandLineToArgs(CommandWithArgs);
string command = argArray[0];
string arg = CommandWithArgs.Substring(Math.Min(command.Length + 1, CommandWithArgs.Length));
return (command, arg);
}
public static void RunSmart(string CommandWithArgs)
{
if (IsUrl(CommandWithArgs))
{
RunAsInvoker(CommandWithArgs, "");
}
else
{
var (command, arg) = SplitCommandArg(CommandWithArgs);
bool showWindow = command.ToLower().Equals("cmd") && arg.IsNullOrEmpty();
if(showWindow) RunAsInvoker(command, arg);
else ProcessInfoChain.New(command, arg).SetWindow(WindowType.ConsoleNoWindow).Excute();
}
}
#region Process Run
public static ProcessInfoChain New(string Command, string Args)
{
return ProcessInfoChain.New(Command, Args);
}
public static ExcuteResult RunAsInvoker(string Command, string Args)
{
return ProcessInfoChain.New(Command, Args).Excute();
}
public static ExcuteResult RunAsLimited(string Command, string Args)
{
return ProcessInfoChain.New(Command, Args).RunAs(Privilege.Limited).Excute();
}
public static ExcuteResult RunAsAdmin(string Command, string Args)
{
return ProcessInfoChain.New(Command, Args).RunAs(Privilege.Admin).Excute();
}
public static ExcuteResult RunWithCmdStart(string Command, string Args)
{
return ProcessInfoChain.New(Command, Args).LaunchBy(LaunchType.CmdStart).Excute();
}
#endregion
}
}