0

I'm using an excellent example here to get my list of drives. It seems to be working but I'm pretty sure I have a logic error as it only lists my last "Local" and my last "Network" drives. If anyone can offer a suggestion, that'd be great.

Here's my code:

protected void Page_Load(object sender, EventArgs e)
{
    DriveInfo[] drives = DriveInfo.GetDrives();
    foreach (DriveInfo drive in drives)
    {
        bool isLocal = IsLocalDrive(drive.Name);
        if (isLocal)
        {
            loc = drive.Name;
        }
        else
        {
            net = drive.Name;
        }            
    }

    local = loc + " ~ ";
    network = net + " ~ ";
}

and

   protected void Button1_Click(object sender, EventArgs e)
   {
      Label1.Text = "Local drives: " + local;
      Label2.Text = "Network drives: " + network;
   }

this produces only:

Local drives: D:\ ~

Network drives: Z:\ ~

Whereas I had expected:

Local drives: A:\ ~ C:\ ~ D:\ ~

Network drives: H:\ ~ I:\ ~ J:\ ~ P:\ ~ U:\ ~ V:\ ~ W:\ ~ X:\ ~ Z:\ ~

Community
  • 1
  • 1
Nallware
  • 159
  • 1
  • 4
  • 18
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 22 '13 at 19:12
  • You know that that is only going to get the drives located on the server and not on the client's machine, right? That's fine as long as that's what you want, but you tagged ASP.Net, so I assume that someone is hitting your server via the web. Not sure what knowing what local server drives are available is going to do for the visitor.... – Michael Todd Oct 22 '13 at 19:15
  • @MichaelTodd I must have misunderstood the example then. I will keep researching because end result would be to have a place for users to save their data but only their networked drives (not local drives) should be selected so the entire 'team' has access. – Nallware Oct 22 '13 at 19:19
  • 2
    What @MichaelTodd means is that you will _never_ be able to get a list of the drives on the client computer by using a web application. That would be a serious security breach. – John Saunders Oct 22 '13 at 19:28
  • This is an intranet application. I'm going to next explore folderbrowser dialog and I want to only show drives that users have available via the local network, not their C:/ hard drive or D:/ DVD Rom drive, or their B:\ floppy drive (for example), and also not drives on the server. – Nallware Oct 22 '13 at 21:06
  • A web browser doesn't care if you're using the Internet or are on an Intr**a**net. You can show the mapped network drives on the server to your user, but that list may or may not be the same as the list of mapped drives on that person's computer. And you will _never_ be able to save to those drives (though you could _upload_ something to the server from them). – Michael Todd Oct 22 '13 at 21:14
  • The functionality for my project will be to allow users to (for example) save a file on a drive they have available. I just want to limit them to local network drives (as opposed to the C:\ drive on their own individual PC) so that other users on the same network can save files to the same drive and directory. – Nallware Oct 23 '13 at 15:35

2 Answers2

3

You're only seeing the last letter because you completely overwrite the string in each iteration of the foreach loop. Instead, you should be appending to the values:

local += string.Format("{0} ~ ", loc);
network += string.Format("{0} ~ ", net);
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3

Your immediate issue is this

Use this instead:

 loc += drive.Name + " ";

 net += drive.Name + " ";

Suggestion:

Use StringBuilder to create strings.

            StringBuilder localSB = new StringBuilder();
            StringBuilder netSB = new StringBuilder();

            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                string loc = string.Empty;
                string net = string.Empty;

                bool isLocal = IsLocalDrive(drive.Name);
                if (isLocal)
                {
                    loc = drive.Name;

                }
                else
                {
                    net = drive.Name;

                }

                if (!String.IsNullOrEmpty(loc))
                {
                    localSB.Append(string.Format("{0} ~ ", loc));
                }
                if (!String.IsNullOrEmpty(net))
                {
                    netSB.Append(string.Format("{0} ~ ", net));
                }
            }

            string localFinal = localSB.ToString();
            string netFinal = netSB.ToString();
granadaCoder
  • 26,328
  • 10
  • 113
  • 146