📄 fileaccess.cs
字号:
using System;
using System.IO;
using System.Xml;
using HardwareDistributor.Business;
using System.Runtime.InteropServices;
using Microsoft.WindowsMobile.Status;
namespace HardwareDistributor.Data
{
/// <summary>
/// Data layer class that interacts with the file system
/// </summary>
public class FileAccess
{
/// <summary>
/// Takes a thrown exception and writes it to a log file
/// </summary>
/// <param name="ex"></param>
public static void LogError(Exception ex)
{
StreamWriter _sw = null;
try
{
//Instantiate an appendable streamwriter that writes to a file called ErrorLog.txt
_sw = new StreamWriter(GlobalCache.Instance.AppPath + "ErrorLog.txt", true, System.Text.Encoding.ASCII);
//Write a row of data containing the time, the error message, and the stack trace
_sw.WriteLine("* " + System.DateTime.Now.ToString() + " | " + ex.Message + " | " + ex.StackTrace);
}
finally
{
//Ensure the streamwriter gets closed
if (_sw != null)
{
_sw.Close();
_sw = null;
}
}
}
/// <summary>
/// Captures device metrics and writes them to a log file
/// </summary>
public static void LogAppState()
{
StreamWriter _sw = null;
try
{
//Get physical and available device memory
uint _storePages = 0;
uint _ramPages = 0;
uint _pageSize = 0;
int _res = GetSystemMemoryDivision(ref _storePages, ref _ramPages, ref _pageSize);
MEMORYSTATUS _memStatus = new MEMORYSTATUS();
GlobalMemoryStatus(_memStatus);
uint _total = _memStatus.dwTotalPhys / 1024;
uint _available = _memStatus.dwAvailPhys / 1024;
//Instantiate an appendable streamwriter that writes to a file called AppStateLog.txt
_sw = new StreamWriter(GlobalCache.Instance.AppPath + "AppStateLog.txt", true, System.Text.Encoding.ASCII);
//Write a row of data containing the time plus the follow device metrics
_sw.WriteLine("* " + System.DateTime.Now.ToString() + " | " +
GlobalCache.Instance.DeviceId + " | " +
GlobalCache.Instance.DeviceName + " | " +
GlobalCache.Instance.IpAddress + " | " +
GlobalCache.Instance.OperatingSystem + " | " +
GlobalCache.Instance.OperatingSystemVersion + " | " +
GlobalCache.Instance.NetCFVersion + " | " +
GlobalCache.Instance.AppVersion + " | " +
SystemState.PowerBatteryStrength.ToString() + " | " +
_available.ToString() + " | " +
_total.ToString());
}
finally
{
//Ensure the streamwrite gets closed
if (_sw != null)
{
_sw.Close();
_sw = null;
}
}
}
/// <summary>
/// Retrieves setting from the xml config file
/// </summary>
/// <param name="settingName"></param>
/// <returns></returns>
public static string GetAppSetting(string appSettingName)
{
//Create an XmlDocument object
XmlDocument _settings = new XmlDocument();
//Open the xml config file
StreamReader _sr = new StreamReader(GlobalCache.Instance.ConfigFile);
//Load the config file into the XmlDocument object
_settings.Load(_sr);
//Close the StreamReader
_sr.Close();
//Iterate through the xml nodes
foreach (XmlNode node in _settings["configuration"]["appSettings"])
{
//Look for a match to the passed in settingName string
if (string.Compare(appSettingName, node.Attributes["key"].Value) == 0)
{
return node.Attributes["value"].Value;
}
}
return "";
}
/// <summary>
/// Writes settings to the xml config file
/// </summary>
/// <param name="settingName"></param>
/// <param name="settingValue"></param>
public static void SetAppSetting(string appSettingName, string appSettingValue)
{
//Create an XmlDocument object
XmlDocument _settings = new XmlDocument();
//Open the xml config file
StreamReader _sr = new StreamReader(GlobalCache.Instance.ConfigFile);
//Load the config file into the XmlDocument object
_settings.Load(_sr);
//Close the StreamReader
_sr.Close();
//Iterate through the xml nodes
foreach (XmlNode node in _settings["configuration"]["appSettings"])
{
//Look for a match to the passed in settingName string
if (string.Compare(appSettingName, node.Attributes["key"].Value) == 0)
{
node.Attributes["value"].Value = appSettingValue;
}
}
//Save the xml config file
_settings.Save(GlobalCache.Instance.ConfigFile);
}
#region P/Invokes
public class MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("CoreDll.dll")]
public static extern void GlobalMemoryStatus
(
MEMORYSTATUS lpBuffer
);
[DllImport("CoreDll.dll")]
public static extern int GetSystemMemoryDivision
(
ref uint lpdwStorePages,
ref uint lpdwRamPages,
ref uint lpdwPageSize
);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -