日期:2014-05-20  浏览次数:20793 次

为什么获取不到Windows操作系统的盘符?
大家好,帮我看下下面这段代码,是教程书上的一个例子,说运行起来可以获取到Windows操作系统的盘符,如C:,D:,E:等。
但是我调试起来,Windows窗体是空的,没有显示Windows操作系统的盘符,这是为什么?我的操作系统是Windows7旗舰版,用的是VS2005。
先在这里谢谢了!
代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace test
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }
  private void Form1_Load(object sender, EventArgs e)
  {
  string[] drives = Directory.GetLogicalDrives();
  for (int i = 0; i < drives.Length; i++)
  {
  listBox1.Items.Add ( drives[i]);
  }
  }
  }
}


------解决方案--------------------
帮你顶 下…………
------解决方案--------------------
同样的代码我这里是成功的
------解决方案--------------------
代码没问题,打断点跟踪一下...

也可以用这个...

DriveInfo[] drives = DriveInfo.GetDrives();

还可以用这个...

string[] drives = Environment.GetLogicalDrives();
------解决方案--------------------
看看窗体属性,点小闪电标志。看Form1_Load事件是不是没注册上,在Form1_Load事件中加断点,看是否执行过。
------解决方案--------------------
测试过可以的,设断点调试下吧~
C# code

string[] GetLogicDir = Directory.GetLogicalDrives();
            foreach (string dir in GetLogicDir)
            {
                listBox1.Items.Add(dir);
            }