-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.cs
More file actions
97 lines (83 loc) · 3 KB
/
Job.cs
File metadata and controls
97 lines (83 loc) · 3 KB
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
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
namespace CmdQueue
{
[Serializable]
[XmlRoot("CmdQueueJob")]
public class Job
{
[XmlElement("Command")]
public string Command { get; set; }
[XmlElement("Arguments")]
public string Arguments { get; set; }
[XmlElement( "StartDirectory")]
public string StartDirectory { get; set; }
[XmlElement( "Name" )]
public string Name { get; set; }
public void SetCommandFromString( string commandString, string argumentsString = "" ) {
ParseCommandLine( commandString );
if ( argumentsString != "" ) {
Arguments = argumentsString + " " + Arguments;
Arguments = Arguments.Trim();
}
}
public void ParseCommandLine( string commandString ) {
string[] parts = SplitCommand( commandString );
Command = parts[0];
Arguments = "";
for( int index = 1; index < parts.Length; index++ ) {
if ( parts[index].Contains(" ") ) {
parts[index] = "\"" + parts[index] + "\"";
}
Arguments += parts[index] + " ";
}
Arguments = Arguments.Trim();
}
public Process GetProcess() {
Process process = new Process();
process.StartInfo.FileName = Command;
process.StartInfo.Arguments = Arguments;
if ( StartDirectory != "" ) {
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = StartDirectory;
}
process.EnableRaisingEvents = true;
return process;
}
private string[] SplitCommand( string CommandToSplit ) {
int numberOfArgs;
IntPtr ptrToSplitArgs;
string[] splitArgs;
ptrToSplitArgs = CommandLineToArgvW( CommandToSplit, out numberOfArgs );
if ( ptrToSplitArgs == IntPtr.Zero )
throw new ArgumentException( "Unable to split argument.",
new Win32Exception() );
try {
splitArgs = new string[numberOfArgs];
for ( int i = 0; i < numberOfArgs; i++ )
splitArgs[i] = Marshal.PtrToStringUni(
Marshal.ReadIntPtr( ptrToSplitArgs, i * IntPtr.Size ) );
return splitArgs;
} finally {
LocalFree( ptrToSplitArgs );
}
}
[DllImport( "shell32.dll", SetLastError = true )]
static extern IntPtr CommandLineToArgvW(
[MarshalAs( UnmanagedType.LPWStr )] string lpCmdLine,
out int pNumArgs );
[DllImport( "kernel32.dll" )]
static extern IntPtr LocalFree( IntPtr hMem );
public override string ToString() {
if ( Name != "" ) {
return Name;
} else {
return Command + " " + Arguments;
}
}
}
}