Click here to Skip to main content
16,011,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to save a file with there name means volume label.
the problem is that i want to save a file in my external hard disk with hd name.

using the code it get the name of external hard disk name:

C#
 DriveInfo[] drives = DriveInfo.GetDrives();
List<string> removables = new List<string>();

foreach (DriveInfo d in drives)
{
    if (d.IsReady == true)
    {
        if (d.DriveType == DriveType.Removable)
        {
            removables.Add(d.VolumeLabel);
            drpExternalDrive.Items.Add(d.VolumeLabel);
        }
    }
}
lets suppose extnl hd name is Nayuma2013.now i want to save it with path "Nayuma2013\Video\NayumaVideo".
So how to do this.
when i use this code then i get error

C#
string adada = "Nayuma2013:\\Video\\NayumaVideo";
if (!Directory.Exists(adada))
{
    Directory.CreateDirectory(adada);
}

file format path is not correct.
Posted
Updated 3-Feb-13 19:20pm
v2

1 solution

This is not a valid directory name. If you are already have valid DriveInfo d for some drive, you can get a valid file name by using System.IO.DriveInfo.RootDirectory property:
C#
string fileName = //... simple file name
fileName = string.Format(
    "{0}{1}{2}",
    d.RootDirectory,
    System.IO.Path.PathSeparator,
    fileName);
Please see: http://msdn.microsoft.com/en-us/library/system.io.driveinfo.rootdirectory%28v=vs.110%29.aspx[^].

If you need the file in some subdirectories, first create them under the root using System.IO.Directory.CreateDirectory:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900