Share via


Current Drive letter?

Question

Wednesday, September 14, 2011 5:53 PM

Hi,

I am trying to build an Installer class to install my desktop app on the clients machine.

I have several items I need to find ... I found a VB example but Microsoft does not give a C# equivilent (that I have found).  (The MS Example is DBCustomAction)

This is going to sound very silly but I am unable to find the object to locate the current Drive letter!  There are lots of commands and samples of getting the Exe path, root, etc, etc,  BUT none that I can find that will give me the current drive letter.

Here is my scinerio as I might be going about this wrong and someone has a better method to offer...

User either (downloads zip file and expands it), runs the expanded msi file from a memory stick or CD...

  1. Locate the drive and path to make sure the files exist?
  2. Get the ComputerName (for SQL)
  3. Build the ConnectionString & connect to SQL (using #2 + "./sqlexpress") using ADO
  4. Using ADO to connect and open a command object
  5. perform a Restore DB (from provided backup)
  6. Execute
  7. Close & done!

Any examples using C# or suggestions are gratefully accepted.

Thanks

SquireDude

All replies (4)

Wednesday, September 14, 2011 6:40 PM âś…Answered | 1 vote

You can get the current directory via Environment.CurrentDirectory, then use Path.GetPathRoot to get the root (drive letter):

 

    string driveLetter = Path.GetPathRoot(Environment.CurrentDirectory);

 

 

Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


Wednesday, September 14, 2011 5:55 PM

Oyu mean to get logical drives?

If so:

string[] drives = System.IO.Directory.GetLogicalDrives();
 
foreach (string drive in drives)
{
    System.IO.DriveInfo info = new System.IO.DriveInfo(drive);
    if (info.DriveType == System.IO.DriveType.Fixed)
    {
        // do what you want with it
    }
}

Mitja


Wednesday, September 14, 2011 6:21 PM

Hi Mitja,

Thanks for the quick come-back....

I have seen thsi example but the assumption is that you already know which drive letter you want!  Id do NOT know which drive letter the user may be using... could be C or D or F or K... that is why I was looking for a method of getting the Current drive letter... the one that the code object is being run from?

SquireDude


Wednesday, September 14, 2011 7:26 PM

Hi Reed,

Thanks got it...

SquireDude