Click here to Skip to main content
12,621,302 members (31,323 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:

 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

string adada = "Nayuma2013:\\Video\\NayumaVideo";
if (!Directory.Exists(adada))
{
    Directory.CreateDirectory(adada);
}
file format path is not correct.
Posted 3-Feb-13 18:55pm
Updated 3-Feb-13 20:20pm
Joezer BH38.7K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100