为什么获取不到Windows操作系统的盘符?

qq402469994 2010-11-24 07:55:39
大家好,帮我看下下面这段代码,是教程书上的一个例子,说运行起来可以获取到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]);
}
}
}
}
...全文
129 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nvmtest 2011-12-03
  • 打赏
  • 举报
回复
我也是在xp系统下,用的系统消息,也获取不到啊。
qq402469994 2010-11-25
  • 打赏
  • 举报
回复
再次检查时,发现Form1窗体事件确实没有加载上,在Form1窗体属性中为Load添加Form1_Load就解决问题了。
谢谢各位楼主啊!
  • 打赏
  • 举报
回复
测试过可以的,设断点调试下吧~

string[] GetLogicDir = Directory.GetLogicalDrives();
foreach (string dir in GetLogicDir)
{
listBox1.Items.Add(dir);
}
qq402469994 2010-11-24
  • 打赏
  • 举报
回复
谢谢大家啊!
我今天再次调试发现了,要在Windows窗体界面上listbox控件上再单击下,Windows操作系统的盘符才会出来。
原本应该会自动出来吧?
兔子-顾问 2010-11-24
  • 打赏
  • 举报
回复
看看窗体属性,点小闪电标志。看Form1_Load事件是不是没注册上,在Form1_Load事件中加断点,看是否执行过。
vrhero 2010-11-24
  • 打赏
  • 举报
回复
代码没问题,打断点跟踪一下...

也可以用这个...

DriveInfo[] drives = DriveInfo.GetDrives();

还可以用这个...

string[] drives = Environment.GetLogicalDrives();
liuwei2500 2010-11-24
  • 打赏
  • 举报
回复
同样的代码我这里是成功的
netstray 2010-11-24
  • 打赏
  • 举报
回复
帮你顶 下…………

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧