获得在C#中的驱动器标签 [英] Get drive label in C#

查看:173
本文介绍了获得在C#中的驱动器标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 System.IO.DriveInfo.GetDrives(),并期待在一个的 .VolumeLabel 属性驱动器,我看到了爱国者XT,这的确是驱动器的卷标。



如果我打开我的电脑,而不是我看TrueCrypt的旅行者磁盘 ,我似乎无法找到任何办法以编程方式检索该值作为没有 DriveInfo 属性认为值。我也试图通过WMI的的Win32_LogicalDisk ,但没有属性包含该值有任何查询信息。



因此​​,任何想法什么我的电脑使用的标签被调用,更重要的是,如何以编程方式找回?



感谢



编辑:要清楚,这里是我使用的代码,后面是什么输出,随后我在我的电脑看(这是我想复制的):

  
的foreach(DriveInfo DI在DriveInfo.GetDrives())
richTextBox1.AppendText((DI.IsReady?(DI.VolumeLabel.Length == 0? DI.DriveType.ToString():DI.VolumeLabel):DI.DriveType.ToString())+(+ DI.Name.Replace(\\,​​)+)+ Environment.NewLine );

可拆卸(A :)
固定(C :)
CDROM(D :)
爱国者XT(E :)
备份(Y :)
数据(Z :)

我的电脑的详细信息视图显示:
软盘驱动器(A :)
本地磁盘(C :)
DVD RW驱动器(D :)
TrueCrypt的旅行者磁盘(E :)
备份(Y :)
数据(Z :)


解决方案

多谢您的autorun.inf提示。下面是我创建检索标签C#片段。

 私人字符串GetDriveLabelFromAutorunInf(字符串DRIVENAME)
{

{
串filepathAutorunInf = Path.Combine(DRIVENAME的Autorun.inf);
串stringInputLine =;
如果(File.Exists(filepathAutorunInf))
{
的StreamReader StreamReader的=新的StreamReader(filepathAutorunInf);
,而((stringInputLine = streamReader.ReadLine())!= NULL)
{
如果(stringInputLine.StartsWith(标签=))
返回stringInputLine.Substring(的startIndex :6);
}
返回;
}
,否则返回;
}
#地区通用catch异常,显示消息框,并终止
赶上(例外的例外)
{
System.Diagnostics.StackTrace跟踪=新System.Diagnostics程序.StackTrace(例外,真正的);
MessageBox.Show(的String.Format({0}异常:\\\
{1} \\\
{2} \\\
\\\
{3} \\\
\\\
Method = {4}行= {5}列= {6},
trace.GetFrame(0).GetMethod()模块,
exception.Message,
exception.StackTrace,
例外。的ToString(),
trace.GetFrame(0).GetMethod()。名称,
trace.GetFrame(0).GetFileLineNumber(),
trace.GetFrame(0).GetFileColumnNumber() ),
错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
Environment.Exit(1);
返回; //让编译器高兴
}
#endregion
}


When I use System.IO.DriveInfo.GetDrives() and look at the .VolumeLabel property of one of the drives, I see "PATRIOT XT", which is indeed the drive's volume label.

If I open "My Computer", instead I see "TrueCrypt Traveler Disk", and I can't seem to find any way to programmatically retrieve that value as none of the DriveInfo properties hold that value. I also tried querying the information via WMI's Win32_LogicalDisk, but no properties contained that value there either.

So any idea what the label My Computer uses is called, and more importantly, how to programmatically retrieve it?

Thanks

EDIT: To be clear, here's the code I'm using, followed by what it outputs, followed by what I see in My Computer (which is what I want to duplicate):


foreach (DriveInfo DI in DriveInfo.GetDrives())
  richTextBox1.AppendText((DI.IsReady ? (DI.VolumeLabel.Length == 0 ? DI.DriveType.ToString() : DI.VolumeLabel) : DI.DriveType.ToString()) + " (" + DI.Name.Replace("\\", "") + ")" + Environment.NewLine);

Removable (A:)
Fixed (C:)
CDRom (D:)
PATRIOT XT (E:)
Backup (Y:)
Data (Z:)

My Computer details view displays:
Floppy Disk Drive (A:)
Local Disk (C:)
DVD RW Drive (D:)
TrueCrypt Traveler Disk (E:)
Backup (Y:)
Data (Z:)

解决方案

Thanks for tip about autorun.inf. Here is the C# fragment which I created to retrieve the label.

private string GetDriveLabelFromAutorunInf(string drivename)
    {
        try
        {
            string filepathAutorunInf = Path.Combine(drivename, "autorun.Inf");
            string stringInputLine = "";
            if (File.Exists(filepathAutorunInf))
            {
                StreamReader streamReader = new StreamReader(filepathAutorunInf);
                while ((stringInputLine = streamReader.ReadLine()) != null) 
                  {
                      if (stringInputLine.StartsWith("label="))
                          return stringInputLine.Substring(startIndex:6);
                  }
                return "";
            }
            else return "";
        }
        #region generic catch exception, display message box, and terminate
        catch (Exception exception)
        {
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(exception, true);
            MessageBox.Show(string.Format("{0} Exception:\n{1}\n{2}\n\n{3}\n\nMethod={4}   Line={5}   Column={6}",
                    trace.GetFrame(0).GetMethod().Module,
                    exception.Message,
                    exception.StackTrace,
                    exception.ToString(),
                    trace.GetFrame(0).GetMethod().Name,
                    trace.GetFrame(0).GetFileLineNumber(),
                    trace.GetFrame(0).GetFileColumnNumber()),
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.Exit(1);
            return "";      // to keep compiler happy
        }
        #endregion
    }

这篇关于获得在C#中的驱动器标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆