首页 > 编程 > C# > 正文

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

2020-01-24 03:01:34
字体:
来源:转载
供稿:网友

打开我的电脑
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得到,我还是更喜欢这种方法

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表