欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#不使用windows api函数打开我的电脑和获取电脑驱动器信息

程序员文章站 2024-02-18 15:28:28
打开我的电脑system.diagnostics.process.start("explorer.exe", "::{20d04fe0-3aea-1069-a2d8-080...

打开我的电脑
system.diagnostics.process.start("explorer.exe", "::{20d04fe0-3aea-1069-a2d8-08002b30309d}");

第二个参数也可为完整路径,

通过查询注册表拿到的一些完整路径例子:

复制代码 代码如下:

public string getwindowspath(string path)
        {
            registrykey folders;
            folders = openregistrypath(registry.currentuser, @"\software\microsoft\windows\currentversion\explorer\shell folders");
            return folders.getvalue(path).tostring();
        }
private registrykey openregistrypath(registrykey root, string s)
        {
            s = s.remove(0, 1) + @"\";
            while (s.indexof(@"\") != -1)
            {
                root = root.opensubkey(s.substring(0, s.indexof(@"\")));
                s = s.remove(0, s.indexof(@"\") + 1);
            }
            return root;
        }


//调用
getwindowspath("关键字");

关键字比如:windows用户桌面路径(desktop),windows用户字体目录路径(fonts),windows用户网络邻居路径(nethood),windows用户我的文档路径(personal)...

具体的在注册表里面查找.

 以上是直接打开我的电脑的方法,当然也可以直接获取我的电脑的驱动器,方法如下:

复制代码 代码如下:

driveinfo[] alldrives = driveinfo.getdrives();

foreach (driveinfo d in alldrives)
{

messagebox.show(d.rootdirectory.fullname+"启动器所有信息在driveinfo对象里都能找到");
messagebox.show(驱动器的类型+drivetypetoch(d.drivetype.tostring()));

}

这里就要说到启动器的类型了

复制代码 代码如下:

#region 驱动类型转换
        /// <summary>
        /// 驱动类型转换
        /// </summary>
        /// <param name="_string"></param>
        /// <returns></returns>
        private string drivetypetoch(string _string)
        {
            string toch = "";
            switch (_string.tolower())
            {
                case "unknown":
                    toch = "其它";
                    break;
                case "removable":
                    toch = "移动设备";
                    break;
                case "fixed":
                    toch = "硬盘";
                    break;
                case "network":
                    toch = "网络硬盘";
                    break;
                case "cdrom":
                    toch = "cd-rom";
                    break;
                case "ram":
                    toch = "ram磁盘";
                    break;
                case "norootdirectory":
                    toch = "";
                    break;
                default:
                    break;
            }
            return toch;
        }
        #endregion

也可以通过windows api得到,我还是更喜欢这种方法