-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
MofFixture.cs
66 lines (58 loc) · 2.11 KB
/
MofFixture.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Xunit;
namespace System.Management.Tests
{
public class MofFixture : IDisposable
{
private Assembly _assembly = typeof(MofFixture).GetTypeInfo().Assembly;
private string _mofCompilerPath = Path.Combine(Environment.SystemDirectory, @"wbem\mofcomp.exe");
public MofFixture()
{
if (AdminHelpers.IsProcessElevated())
ExtractAndCompileMof("WmiEBvt.mof");
}
public void Dispose()
{
if (AdminHelpers.IsProcessElevated())
ExtractAndCompileMof("CleanUp.mof");
}
private void ExtractAndCompileMof(string mofResourceName)
{
string mofFilePath = null;
try
{
mofFilePath = ExtractMofFromResourcesToFile(mofResourceName);
CompileMof(mofFilePath);
}
finally
{
if (mofFilePath != null)
File.Delete(mofFilePath);
}
}
private string ExtractMofFromResourcesToFile(string mofResourceName)
{
var mofFilePath = Path.Combine(Path.GetTempPath(), mofResourceName);
using (var mofStream = new StreamReader(_assembly.GetManifestResourceStream(mofResourceName)))
{
string mofContent = mofStream.ReadToEnd();
File.WriteAllText(mofFilePath, mofContent);
}
return mofFilePath;
}
private void CompileMof(string mofFilePath)
{
var psi = new ProcessStartInfo(_mofCompilerPath, mofFilePath);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
Assert.True(p.ExitCode == 0, $"Failed to compile mof file {mofFilePath} output: {output}");
}
}
}