-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTempDirectory.cs
More file actions
128 lines (122 loc) · 7.72 KB
/
TempDirectory.cs
File metadata and controls
128 lines (122 loc) · 7.72 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using BytecodeApi.Data;
using System.Diagnostics;
namespace BytecodeApi.IO;
/// <summary>
/// Helper class for interoperability with the current user's temporary folder.
/// </summary>
public static class TempDirectory
{
/// <summary>
/// Creates a subdirectory in the current user's temporary folder named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" />.
/// </summary>
/// <returns>
/// A <see cref="string" /> value with the full path to the created directory.
/// </returns>
public static string CreateDirectory()
{
string path = Path.Combine(Path.GetTempPath(), Create.Guid(GuidFormat.Braces));
Directory.CreateDirectory(path).Attributes |= FileAttributes.NotContentIndexed;
return path;
}
/// <summary>
/// Creates a file in the current user's temporary folder with the specified filename and writes the value of <paramref name="content" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />.
/// </summary>
/// <param name="fileName">A <see cref="string" /> value specifying the filename.</param>
/// <param name="content">A <see cref="byte" />[] specifying the content that is written to the file.</param>
/// <returns>
/// A <see cref="string" /> value with the full path to the created file.
/// </returns>
public static string CreateFile(string fileName, byte[] content)
{
Check.ArgumentNull(fileName);
Check.ArgumentNull(content);
string path = Path.Combine(CreateDirectory(), fileName);
File.WriteAllBytes(path, content);
new FileInfo(path).Attributes |= FileAttributes.NotContentIndexed | FileAttributes.Temporary | FileAttributes.ReadOnly;
return path;
}
/// <summary>
/// Creates a file in the current user's temporary folder with the name of the <see cref="Blob" /> and writes the content of the <see cref="Blob" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />.
/// </summary>
/// <param name="file">A <see cref="Blob" /> with the filename and its content.</param>
/// <returns>
/// A <see cref="string" /> value with the full path to the created file.
/// </returns>
public static string CreateFile(Blob file)
{
Check.ArgumentNull(file);
Check.ArgumentNull(file.Name);
Check.ArgumentEx.StringNotEmpty(file.Name);
Check.ArgumentNull(file.Content);
return CreateFile(file.Name, file.Content);
}
/// <summary>
/// Creates a file in the current user's temporary folder with the specified filename and writes the value of <paramref name="content" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />. The file is then executed and the <see cref="Process" /> is returned and <see langword="null" />, if the file could not be executed.
/// </summary>
/// <param name="fileName">A <see cref="string" /> value specifying the filename.</param>
/// <param name="content">A <see cref="byte" />[] specifying the content that is written to the file.</param>
/// <returns>
/// The <see cref="Process" /> of the executed file and
/// <see langword="null" />, if <see cref="Process" /> creation failed.
/// </returns>
public static Process? ExecuteFile(string fileName, byte[] content)
{
return ExecuteFile(fileName, content, false);
}
/// <summary>
/// Creates a file in the current user's temporary folder with the specified filename and writes the value of <paramref name="content" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />. The file is then executed and the <see cref="Process" /> is returned and <see langword="null" />, if the file could not be executed.
/// </summary>
/// <param name="fileName">A <see cref="string" /> value specifying the filename.</param>
/// <param name="content">A <see cref="byte" />[] specifying the content that is written to the file.</param>
/// <param name="runas"><see langword="true" /> to execute this file with the "runas" verb.</param>
/// <returns>
/// The <see cref="Process" /> of the executed file and
/// <see langword="null" />, if <see cref="Process" /> creation failed.
/// </returns>
public static Process? ExecuteFile(string fileName, byte[] content, bool runas)
{
Check.ArgumentNull(fileName);
Check.ArgumentNull(content);
try
{
return Process.Start(new ProcessStartInfo(CreateFile(fileName, content))
{
UseShellExecute = true,
Verb = runas ? "runas" : ""
});
}
catch
{
return null;
}
}
/// <summary>
/// Creates a file in the current user's temporary folder with the name of the <see cref="Blob" /> and writes the content of the <see cref="Blob" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />. The file is then executed and the <see cref="Process" /> is returned and <see langword="null" />, if the file could not be executed.
/// </summary>
/// <param name="file">A <see cref="Blob" /> with the filename and its content.</param>
/// <returns>
/// The <see cref="Process" /> of the executed file and
/// <see langword="null" />, if <see cref="Process" /> creation failed.
/// </returns>
public static Process? ExecuteFile(Blob file)
{
return ExecuteFile(file, false);
}
/// <summary>
/// Creates a file in the current user's temporary folder with the name of the <see cref="Blob" /> and writes the content of the <see cref="Blob" /> to it. The file is created in a subdirectory named by a <see cref="Guid" /> with the <see cref="GuidFormat.Braces" /> format. The subdirectory is created with <see cref="FileAttributes.NotContentIndexed" /> and the file's attributes are set to <see cref="FileAttributes.NotContentIndexed" /> | <see cref="FileAttributes.Temporary" />. The file is then executed and the <see cref="Process" /> is returned and <see langword="null" />, if the file could not be executed.
/// </summary>
/// <param name="file">A <see cref="Blob" /> with the filename and its content.</param>
/// <param name="runas"><see langword="true" /> to execute this file with the "runas" verb.</param>
/// <returns>
/// The <see cref="Process" /> of the executed file and
/// <see langword="null" />, if <see cref="Process" /> creation failed.
/// </returns>
public static Process? ExecuteFile(Blob file, bool runas)
{
Check.ArgumentNull(file);
Check.ArgumentNull(file.Name);
Check.ArgumentEx.StringNotEmpty(file.Name);
Check.ArgumentNull(file.Content);
return ExecuteFile(file.Name, file.Content, runas);
}
}