获取安装目录、临时目录等系统信息的C#源代码
using System;
using System.Management;
using System.Threading;
namespace Soyee.Comm
{
/// Operate MetaSystem information
/// </summary>
public class Software
{
public string OSType;
public string OSVersion;
public string WinTempDir;
public string WinDir;
public string SystemDirectory;
/// <summary>
/// Aplication file directory ,postfix is "\" .for example, D:\Test\Bin\
/// </summary>
public string AplicationDir;
public string AplicationName;
/// <summary>
/// Aplication root directory,postfix is "\" . for example, D:\Test\
/// </summary>
public string InstallDirectory;
public string UserDomainName;
public long WorkingMemory;
private static Software _instance;
public static Software Instance()
{
if (_instance == null)
_instance = new Software();
return _instance;
}
public Software()
{
OSType= System.Environment.GetEnvironmentVariable("OS");
WinTempDir=System.Environment.GetEnvironmentVariable("TEMP");
WinDir=System.Environment.GetEnvironmentVariable("WINDIR");
AplicationDir=System.Environment.CurrentDirectory+@"\";
OSVersion=System.Environment.OSVersion.ToString();
SystemDirectory=System.Environment.SystemDirectory;
UserDomainName=System.Environment.UserDomainName;
WorkingMemory=System.Environment.WorkingSet;
InstallDirectory=AplicationDir.Replace(@"\bin\Debug","").Replace(@"\bin\Release","");
AplicationName=GetAplicationName();
}
public string GetInstallDir()
{
string dir;
dir=AplicationDir;
for (int i = 0; i<3;i++)
{
dir=dir.Substring(0,dir.LastIndexOf(@"\"));
}
return dir+@"\";
}
string GetAplicationName()
{
try
{
//获取应用程序名称
string st="";
ManagementClass mc = new ManagementClass("Win32_Process");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
st=mo["Caption"].ToString();
}
//moc.Dispose();
//mc.Dispose();
moc=null;
mc=null;
return st;
}
finally
{
}
}
}
}