/
LogFile.cs
183 lines (151 loc) · 5.25 KB
/
LogFile.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2015, Ben Baker
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Management;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CPWizard
{
public class LogFile
{
private static string m_fileName = null;
private static object m_lockObject = null;
public delegate void LogFileDelegate(string errorString);
public static LogFileDelegate LogFileEvent = null;
static LogFile()
{
m_lockObject = new object();
}
public static void ClearLog()
{
if (String.IsNullOrEmpty(m_fileName))
return;
try
{
lock (m_lockObject)
{
string folder = Path.GetDirectoryName(m_fileName);
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
using (System.IO.StreamWriter streamWriter = File.CreateText(m_fileName))
streamWriter.Flush();
}
}
catch (Exception)
{
//System.Windows.Forms.MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
public static void VerboseWriteLine(string methodName, string className, string errorString)
{
if (Settings.General.VerboseLogging)
WriteLine(String.Format("{0} ({1}) -> {2}", methodName, className, errorString));
}
public static void VerboseWriteLine(string errorString)
{
if (Settings.General.VerboseLogging)
WriteLine(errorString);
}
public static void WriteLine(string methodName, string className, string errorString)
{
WriteLine(String.Format("ERROR @ {0} ({1})", methodName ?? "", className ?? ""));
WriteLine(errorString ?? "");
}
public static void WriteLine(string methodName, string className, string errorString, string stackTrace)
{
WriteLine(String.Format("ERROR @ {0} ({1})", (methodName ?? ""), className ?? ""));
WriteLine(errorString ?? "");
WriteLine(stackTrace ?? "");
}
public static void WriteLine(string errorString, string stackTrace)
{
WriteLine(String.Format("{0} : {1}", errorString ?? "", stackTrace ?? ""));
}
public static void WriteLine(string format, params object[] args)
{
if (String.IsNullOrEmpty(m_fileName))
return;
try
{
lock (m_lockObject)
{
using (System.IO.StreamWriter streamWriter = File.AppendText(m_fileName))
{
streamWriter.WriteLine(String.Format("{0}: {1}", DateTime.Now, String.Format(format, args)));
streamWriter.Flush();
}
}
if (LogFileEvent != null)
LogFileEvent(String.Format(format, args));
}
catch (Exception)
{
//System.Windows.Forms.MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
public static void OutputSystemConfiguration()
{
try
{
//WriteLine("CPWizard " + Globals.Version); //moved to main program in case config parse disabled
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject mo in query.Get())
{
Double totalRAM = Double.Parse(mo["TotalVisibleMemorySize"].ToString()) / 1024;
Double freeRAM = Double.Parse(mo["FreePhysicalMemory"].ToString()) / 1024;
WriteLine("OS: " + mo["Caption"].ToString());
WriteLine("Version: " + mo["Version"].ToString());
WriteLine("Build: " + mo["BuildNumber"].ToString());
WriteLine("RAM Total: " + Math.Ceiling(totalRAM) + " MB");
WriteLine("RAM Used: " + Math.Ceiling(totalRAM - freeRAM) + " MB");
}
query = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject obj in query.Get())
WriteLine("CPU: " + obj["Name"].ToString().TrimStart());
query = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (ManagementObject obj in query.Get())
{
WriteLine("Video Card: " + obj["Name"].ToString());
WriteLine("Video Driver: " + obj["DriverVersion"].ToString());
if (obj["AdapterRAM"] != null)
{
double videoRAM = double.Parse(obj["AdapterRAM"].ToString()) / 1024 / 1024;
WriteLine("Video RAM: " + videoRAM + " MB");
}
}
query = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
foreach (ManagementObject obj in query.Get())
{
WriteLine("Sound Card: " + obj["Name"].ToString());
}
if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\policy\\v1.0") != null)
WriteLine(".NET: .NET Framework 1.0 Installed");
if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\policy\\v1.1") != null)
WriteLine(".NET: .NET Framework 1.1 Installed");
if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\policy\\v2.0") != null)
WriteLine(".NET: .NET Framework 2.0 Installed");
if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\policy\\v3.0") != null)
WriteLine(".NET: .NET Framework 3.0 Installed");
query.Dispose();
query = null;
}
catch (Exception)
{
//MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
}
}
public static string FileName
{
get { return m_fileName; }
set { m_fileName = value; }
}
}
}