检测硬盘信息,扫描硬盘坏道——WinForm

前一阵子在弄一个硬盘检测程序,在网上搜索过,不是资料不全就是含糊其词。今天有幸能在上面说说,不足之处还希望大家多多指教。

本人也是通过网络资源搜索到相关资料,今天整理了发到网上。(如果对此有什么建议可以Email给我!)

 

首先准备工作,导入必须的命名空间:

1 using System.Diagnostics;
2 using System.IO;
3 using System.Threading;
4 using System.Management;

 

然后在窗体Form_Load编写代码:

 

1 Form5 frm = new Form5();//声明检测窗体
2 private void Form4_Load(object sender, EventArgs e)
3 {
4    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//是否调用错误线程
5    frm.EventHand += new EventHandler(SetScanInfo);//为测试窗体添加事件
6    GetDskInfo();                                  //获取硬盘大小等信息
7    GetDskWd();                                    //获取硬盘温度
8    comboBoxEx1.SelectedIndex = 0;
9 }

 

 

下面将依次贴出Form_Load中所调用方法:
获取硬盘分区信息
      private void GetDskInfo()
        {
            SelectQuery selectQuery 
= new SelectQuery("select * from win32_logicaldisk");
            ManagementObjectSearcher searcher 
= new ManagementObjectSearcher(selectQuery);
            
foreach (ManagementObject disk in searcher.Get())
            {
                
//获取驱动器盘符
                if (disk["DriveType"].ToString().Equals("3"))
                {
                    ComboBoxItem newItem 
= new ComboBoxItem();
                    newItem.Text 
= disk["VolumeName"].ToString() + "(" + disk["Name"].ToString() + ")" + "    [" + GetSizeUseUnit(disk["Size"].ToString()) + "]";
                    newItem.Value 
= disk["Name"].ToString();
                    comboBoxEx1.Items.Add(newItem);
                }
            }
        }

 

硬盘大小格式化
      private string GetSizeUseUnit(string size)
        {
            
double dSpace = Convert.ToDouble(size);
            
string sSpace = dSpace.ToString("N");
            
string[] tmp;
            
string rtnSize = "0";
            tmp 
= sSpace.Split(',');
            
switch (tmp.GetUpperBound(0))
            {
                
case 0:
                    rtnSize 
= tmp[0+ " 字节";
                    
break;
                
case 1:
                    rtnSize 
= tmp[0+ "." + tmp[1].Substring(02+ " K";
                    
break;
                
case 2:
                    rtnSize 
= tmp[0+ "." + tmp[1].Substring(02+ " M";
                    
break;
                
case 3:
                    rtnSize 
= tmp[0+ "." + tmp[1].Substring(02+ " G";
                    
break;
                
case 4:
                    rtnSize 
= tmp[0+ "." + tmp[1].Substring(02+ " T";
                    
break;
            }
            
return rtnSize;
        }

 

获取硬盘温度
      private string GetTemp()
        {
            
try
            {
                
string temperatur = String.Empty;
                
string gesamt = String.Empty;

                ManagementObjectSearcher searcher 
= new ManagementObjectSearcher("root\\WMI""SELECT * FROM MSStorageDriver_ATAPISmartData");
                ManagementObjectSearcher logicDisk 
= new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

                
foreach (ManagementObject queryObjDisk in logicDisk.Get())
                {
                    
string DiskName = Convert.ToString(queryObjDisk["Model"]);

                    
foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Byte[] arrVendorSpecific 
= (Byte[])(queryObj["VendorSpecific"]);

                        
if (DiskName.IndexOf("Maxtor">= 0)//迈拓硬盘
                        {
                            temperatur 
= Convert.ToString(arrVendorSpecific[151]);
                            gesamt 
= DiskName + " " + temperatur + "°C";
                            
break;
                        }
                        
else
                        {
                            
if ((DiskName.IndexOf("Western Digital">= 0|| (DiskName.IndexOf("WesternDigital">= 0|| (DiskName.IndexOf("WD">= 0))//西部数据
                            {
                                temperatur 
= Convert.ToString(arrVendorSpecific[139]);
                                gesamt 
= DiskName + " " + temperatur + "°C";
                                
break;
                            }
                            
else if ((DiskName.IndexOf("Seagate">= 0|| (DiskName.IndexOf("ST">= 0|| (DiskName.IndexOf("Seagate Technology">= 0))//希捷
                            {
                                temperatur 
= Convert.ToString(arrVendorSpecific[127]);
                                gesamt 
= DiskName + " " + temperatur + "°C";
                                
break;
                            }
                            
else if ((DiskName.IndexOf("FT">= 0|| (DiskName.IndexOf("FUJITSU">= 0))//富士通硬盘
                            {
                                temperatur 
= Convert.ToString(arrVendorSpecific[151]);
                                gesamt 
= DiskName + " " + temperatur + "°C";
                                
break;
                            }
                        }

                    }
                }

                
return gesamt;
            }
            
catch (ManagementException err)
            {
                
return err.Message;
            }
        }

 

ComboBox处理类
   public class ComboBoxItem
    {
        
private string _text = null;
        
private object _value = null;
        
public string Text { get { return this._text; } set { this._text = value; } }
        
public object Value { get { return this._value; } set { this._value = value; } }
        
public override string ToString()
        {
            
return this._text;
        }
    }

 

检测画面所调用方法
       void SetScanInfo(object sender, EventArgs e)
        {
            frm 
= (Form5)sender;
            
if (null != frm.EventHand)
            {
                textBoxX1.Text 
+= frm.Content + "\r\n";
            }

            
this.Focus();
        }

 


到此获取硬盘信息所有重要方法都已贴出,下面将贴出硬盘扫描画面代码:
在检测画面中所需要引用的命名空间:
1 using System.Threading;
2 using System.Diagnostics;
3 using System.IO;

 

测试窗体中代码
 Thread mythread;

        
private void timer1_Tick(object sender, EventArgs e)
        {
            
if (this.progressBarX1.Value < 100)
            {
                
this.progressBarX1.PerformStep();
            }
            
else
            {
                
this.progressBarX1.Value = 0;
                
this.label1.Text = (int.Parse(this.label1.Text) + 1).ToString();
            }

        }

        
private void Form5_Load(object sender, EventArgs e)
        {
            
this.progressBarX1.Capture = false;
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls 
= false;
            mythread 
= new Thread(new ThreadStart(WriteData));
            mythread.Start();
        }

        
private void WriteData()
        {
            
try
            {
                ProcessStartInfo start 
= new ProcessStartInfo("cmd.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                start.Arguments = "/c chkdsk " + Ml;//设置命令参数
                start.CreateNoWindow = true;//不显示dos命令行窗口
                start.RedirectStandardOutput = true;//
                start.RedirectStandardInput = true;//
                start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
                Process p = Process.Start(start);
                StreamReader reader 
= p.StandardOutput;//截取输出流
                string line = reader.ReadLine();//每次读取一行
                while (!reader.EndOfStream)
                {
                    line 
= reader.ReadLine();
                    Content 
= line + " ";
                    
if (null != EventHand)
                    {
                        
try
                        {
                            EventHand(
this, EventArgs.Empty);
                        }
                        
catch
                        {
                            EventHand(
this, EventArgs.Empty);
                        }
                    }
                }
                p.WaitForExit();
//等待程序执行完退出进程
                p.Close();//关闭进程
                reader.Close();//关闭流

                
if (mythread.IsAlive)
                {
                    mythread.Abort();
                }
            }
            
catch
            {
                
this.Close();
                mythread.Abort();
            }
        }

        
private string m_content = String.Empty;
        
private EventHandler m_eventHand;
        
private string m_ml = String.Empty;

        
public string Ml
        {
            
get { return m_ml; }
            
set { m_ml = value; }
        }

        
public EventHandler EventHand
        {
            
get { return m_eventHand; }
            
set { m_eventHand = value; }
        }

        
public string Content
        {
            
get { return m_content; }
            
set { m_content = value; }
        }

 

 

 

最后按F5运行,效果画面如下

                                                效果图(—)

点击检测按钮后:

                                          效果图(二)

 

好了到此就算完成,希望对您有所帮助。

posted @ 2009-12-22 11:43  gonganruyi  阅读(1206)  评论(0编辑  收藏  举报