Get drive label in C#

前端 未结 8 1994
臣服心动
臣服心动 2020-12-20 12:20

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 dr

8条回答
  •  囚心锁ツ
    2020-12-20 12:39

    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
        }
    

提交回复
热议问题