Get file path

J

John Denver

Hello

I'm writing up a C# utiliy that basically copies file from a CD rom on the
the hard drive. The file names and paths that are on the CR are not known
to me. All I'm going to do is get the file path and name then check for it
on the local hard drive.

My first though would be that I would have to get the drive letter of the CD
Rom from the DriveInfo class. I want to write to a log file the path and
file name that I find on the CR and copy to the local disk.

How do I grab the file name and path from the CD rom?

Thanks
 
J

Jeff Johnson

See Directory.GetFiles().

Don't forget that many computers have multiple optical drives.

And before you think, "I doubt my users do," consider all the virtual CD
programs out there. They make it LOOK like there are multiple optical drives
even though there may be only one physical drive in the machine. So it's
more important to follow Pete's advice nowadays than it ever was before.
 
J

John Denver

Now how do I go about getting the full path?

The DirectoryInfo[] diArr = di.GetDirectories();

Returns me an array of folders on a particular drive, but how do I get the
subfolders within that folder?

Thanks
 
R

Rob Lancaster

John Denver said:
Now how do I go about getting the full path?

The DirectoryInfo[] diArr = di.GetDirectories();

Returns me an array of folders on a particular drive, but how do I get the
subfolders within that folder?

Thanks

The following will get you the full path of every folder under a start
folder (in the example, My Music)...

using System;
using System.Collections.Generic;
using System.IO;

namespace GetFolders
{
class Program
{
static List<DirectoryInfo> folderList = new List<DirectoryInfo>();
static string basePath =
Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);

static void Main()
{
MakeDirectoryList(basePath);
}

static void MakeDirectoryList(string startDirectory)
{
DirectoryInfo home = new DirectoryInfo(startDirectory);
folderList.Add(home);

DirectoryInfo di = new DirectoryInfo(startDirectory);
DirectoryInfo[] subDirs = di.GetDirectories();

foreach (DirectoryInfo temp in subDirs)
{
folderList.Add(temp);
string newRootDirectory = temp.FullName.ToString();
MakeDirectoryList(newRootDirectory);
}
}
}
}

to work with files in these folders,

static void WorkWithFiles()
{
foreach (DirectoryInfo di in folderList)
{
FileInfo[] myFiles = di.GetFiles();

foreach (FileInfo thisFile in myFiles)
{
//do something with "thisFile"
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top