取硬盘序列号:
public string GetClientVolumnSN()
{
try
{
string dirresults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "fsutil";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "fsinfo volumeinfo c:";
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirresults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
dirresults = dirresults.Replace("/r", "").Replace("/n", "").Replace("/t", "");
Regex reg = new Regex("卷序列号[ ]{0,}:[ ]{0,}(?<key>((.)*?))__volumnsn", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match mc = reg.Match(dirresults + "__volumnsn");
if (mc.Success)
{
return mc.Groups["key"].Value.Substring(0, 10);
}
else
{
return "";
}
}
catch
{
return "";
}
}
取网卡MAC地址:
以下的IP可由Request.UserHostAddress取得,自己可用ipconfig取得的ip先做测试。
public string GetClientMAC(string IP)
{
try
{
string dirresults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-a " + IP;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirresults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
dirresults = dirresults.Replace("/r", "").Replace("/n", "").Replace("/t", "");
Regex reg = new Regex("mac[ ]{0,}address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__mac", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match mc = reg.Match(dirresults + "__mac");
if (mc.Success)
{
return mc.Groups["key"].Value.Substring(0, 17);
}
else
{
reg = new Regex("host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
mc = reg.Match(dirresults);
if (mc.Success)
{
return "Host not found!";
}
else
{
return "";
}
}
}
catch
{
return "";
}
}
本文提供了两段C#源码,分别用于在ASP.NET环境中获取客户端计算机的硬盘序列号和网卡MAC地址。通过使用`fsutil`和`nbtstat`命令行工具,并结合正则表达式解析输出结果,实现了这两项功能。代码中包含了异常处理以确保稳定运行。
741

被折叠的 条评论
为什么被折叠?



