/[DiskWriteCacheDisabler]/trunk/libDiskpart/Diskpart/Interop.cs
ViewVC logotype

Contents of /trunk/libDiskpart/Diskpart/Interop.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 51 - (show annotations) (download)
Thu Aug 9 07:44:20 2012 UTC (7 years, 5 months ago) by william
File size: 6355 byte(s)
+ add support to enable/disable device enumeration methods

1 #define LIBDISKPART_ENABLE_UNITTESTING // when defined will log diskpart ouput
2 #region t
3 #define LIBDISKPART_ENABLE_VOLUME_ENUMERATION // when defined will enable method ListVolumes()
4 //#define LIBDISKPART_ENABLE_DISK_ENUMERATION // when defined will enable method ListDisks()
5 //#define LIBDISKPART_ENABLE_VIRTUALDISK_ENUMERATION // when defined will enable method ListVirtualDisks()
6 //#define LIBDISKPART_ENABLE_PARTITION_ENUMERATION // when defined will enable method ListPartitions()
7 #endregion
8
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Diagnostics;
14 using System.Reflection;
15 using System.IO;
16
17 namespace libDiskpart.Diskpart
18 {
19 public static class Interop
20 {
21 #region temporary file creation
22 private const string temporary_filename = "libdiskpart_tmp_file.tmp";
23 private static string GetTemporaryPath()
24 {
25 Assembly t = typeof(libDiskpart.Globals).Assembly;
26 string path = t.Location.Replace(@"\libDiskpart.dll", "");
27 return path;
28 }
29 internal static string CreateTemporaryFile(string text)
30 {
31 string path = GetTemporaryPath();
32 FileInfo fi = new FileInfo(string.Format(@"{0}\{1}", path, temporary_filename));
33 using (FileStream fs = new FileStream(fi.FullName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read))
34 {
35 using (StreamWriter sw = new StreamWriter(fs))
36 {
37 sw.AutoFlush = true;
38 sw.Write(text);
39 sw.Close();
40 }
41 }
42 return fi.FullName;
43 }
44 internal static void DeleteTemporaryFile()
45 {
46 string path = GetTemporaryPath();
47 FileInfo fi = new FileInfo(string.Format(@"{0}\{1}", path, temporary_filename));
48 if (fi.Exists)
49 fi.Delete();
50 }
51 #endregion
52
53 #region private static string CreateDiskpartTestScriptCommand(string command)
54 private static string CreateDiskpartTestScriptCommand(params string[] commands)
55 {
56 string script_filename = "diskpart_tmp_script";
57 Assembly t = typeof(libDiskpart.Globals).Assembly;
58 string path = t.Location.Replace(@"\libDiskpart.dll", "");
59 FileInfo fi = new FileInfo(string.Format(@"{0}\{1}", path, script_filename));
60 StringBuilder builder = new StringBuilder();
61 commands.ToList().ForEach(x => builder.AppendLine(x));
62 DeleteTemporaryFile();
63 string tmp_file = CreateTemporaryFile(builder.ToString());
64 return tmp_file;
65 }
66 #endregion
67 #region private static List<string> CreateDiskpartProcess(params string[] commands)
68 private static List<string> CreateDiskpartProcess(params string[] commands)
69 {
70 List<string> std_ouput_list = new List<string>();
71 string tmp_file = CreateDiskpartTestScriptCommand(commands);
72 Process p = new Process();
73 p.StartInfo.FileName = Globals.DiskpartFilename;
74 p.StartInfo.Arguments = string.Format("/s \"{0}\"", tmp_file);
75 //p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
76 p.StartInfo.CreateNoWindow = true;
77 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
78 p.StartInfo.UseShellExecute = false;
79 p.StartInfo.RedirectStandardError = true;
80 p.StartInfo.RedirectStandardInput = true;
81 p.StartInfo.RedirectStandardOutput = true;
82 p.Start();
83 //p.BeginOutputReadLine();
84 //p.BeginErrorReadLine();
85 p.WaitForExit();
86
87 DeleteTemporaryFile();
88
89 StringReader sr = new StringReader(p.StandardOutput.ReadToEnd());
90 string line = string.Empty;
91 while ((line = sr.ReadLine()) != null)
92 {
93 if (line.ToLower().StartsWith("Microsoft".ToLower()) ||
94 line.ToLower().StartsWith("Copyright".ToLower()) ||
95 line.ToLower().StartsWith("On computer:".ToLower()) ||
96 line == string.Empty)
97 {
98 continue;
99 }
100 std_ouput_list.Add(line);
101 }
102 return std_ouput_list;
103 }
104 #endregion
105 [Conditional("LIBDISKPART_ENABLE_UNITTESTING")]
106 private static void LogOutput(List<string> output) { output.ForEach(x => Console.WriteLine(x)); }
107
108
109 #region Diskpart List Support
110 public static void EnumerateVolumes()
111 {
112 #if !LIBDISKPART_ENABLE_VOLUME_ENUMERATION
113 Console.WriteLine("WARNING: Volume enumeration has not been enabled!");
114 return;
115 #else
116 List<string> output = CreateDiskpartProcess("list vol");
117 List<IVolumeInformation> collection = new List<IVolumeInformation>();
118 VolumeInformation.Parse(output, out collection);
119 LogOutput(output);
120 #endif
121 }
122 public static void EnumerateDisks()
123 {
124 #if !LIBDISKPART_ENABLE_DISK_ENUMERATION
125 Console.WriteLine("WARNING: Disk enumeration has not been enabled!");
126 return;
127 #else
128 List<string> output = CreateDiskpartProcess("list disk");
129 LogOutput(output);
130 #endif
131 }
132 public static void EnumerateVirtualDisks()
133 {
134 #if !LIBDISKPART_ENABLE_VIRTUALDISK_ENUMERATION
135 Console.WriteLine("WARNING: Virtual Disk enumeration has not been enabled!");
136 return;
137 #else
138 List<string> output = CreateDiskpartProcess("list vdisk");
139 LogOutput(output);
140 #endif
141 }
142 public static void EnumeratePartitions(int volume_number)
143 {
144 #if !LIBDISKPART_ENABLE_PARTITION_ENUMERATION
145 Console.WriteLine("WARNING: Partition enumeration has not been enabled!");
146 return;
147 #else
148 List<string> output = CreateDiskpartProcess(string.Format("sel vol {0}", volume_number), "list part");
149 LogOutput(output);
150 #endif
151 }
152
153 #endregion
154
155 }
156 }

  ViewVC Help
Powered by ViewVC 1.1.22