Link to home
Start Free TrialLog in
Avatar of Todd710
Todd710

asked on

Why can't I get Network drive info with elevated permissions?

I am providing code to show you the problem.  I have network drives on my system and when I run this app under the debugger in Visual Studio 2010 the result is nothing.  If I take the executable aside and run it as Administrator still nothing.  But if I do not elevate the app the results are there. What am I doing wrong?  Why would elevating an app cause it to restrict the results?  That makes no sense.  Thanks for any help!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Management;

namespace NetworkDrives
{
    public partial class NetworkDriveTest : Form
    {
        public NetworkDriveTest()
        {
            InitializeComponent();
        }

        private void btnGetNetworkDriveInfo_Click(object sender, EventArgs e)
        {
            GetNetworkDriveStats("P:\\");
        }

        private void GetNetworkDriveStats(string Drive)
        {
            bool bDriveAvailable = false;
            string sRemoteBackupDrive = Path.GetPathRoot(Drive);
            lbxDriveStats.Items.Add("****Mapped Hard Drive****");
            lbxDriveStats.Items.Add("");
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_LogicalDisk where DriveType=4");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    string sDriveName = queryObj["Name"].ToString();
                    if (sRemoteBackupDrive.Contains(sDriveName))
                    {
                        lbxDriveStats.Items.Add("Drive " + queryObj["Name"]);
                        lbxDriveStats.Items.Add("  Drive type: \t\t\t" + queryObj["Description"]);
                        lbxDriveStats.Items.Add("  Volume label: \t\t\t" + queryObj["VolumeName"]);
                        lbxDriveStats.Items.Add("  File system: \t\t\t" + queryObj["FileSystem"]);
                        lbxDriveStats.Items.Add("  Compressed: \t\t\t" + queryObj["Compressed"]);
                        lbxDriveStats.Items.Add("  UNC Path: \t\t\t" + queryObj["ProviderName"]);
                        lbxDriveStats.Items.Add("  Quota Disabled: \t\t\t" + queryObj["QuotasDisabled"]);
                        lbxDriveStats.Items.Add(
                            "  Total available space: \t\t" + queryObj["FreeSpace"] + " bytes");
                        lbxDriveStats.Items.Add(
                            "  Total size of drive: \t\t" + queryObj["Size"] + " bytes ");

                        lbxDriveStats.Items.Add("");
                        lbxDriveStats.Items.Add("=====================================================");
                        lbxDriveStats.Items.Add("");
                        bDriveAvailable = true;
                    }
                    if (!bDriveAvailable)
                    {
                        lbxDriveStats.Items.Add("  ERROR: Drive Was not in a ready state.");

                        lbxDriveStats.Items.Add("");
                        lbxDriveStats.Items.Add("=====================================================");
                        lbxDriveStats.Items.Add("");
                    }
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
            lbxDriveStats.Items.Add("Queried Drives");
        }
    }
}

Open in new window

9-21-2011-3-10-49-PM.png
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Mapped drive do not have 2 slashes in their path. Replace ("P:\\") by ("P:\")
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd710
Todd710

ASKER

I am doubtful that the user is different that would cause way to many issues.

I appreciate your help and I will give GetDrives() a look.  But I really need to know why this is happening.  Surely I am not the only person that has had this issue before.  
Avatar of Todd710

ASKER

Additional info I just ran the application using GetDrives() with the same result see screenshots.
9-22-2011-9-33-03-AM.png
9-22-2011-9-31-06-AM.png
Avatar of Todd710

ASKER

@tgerbert

I am giving you credit for this question based on your answer the the issue was: "Perhaps by elevating the program you are causing it to run as a different user, presumably the Administrator".

However, the answer is that it is the same user different access tokens related to the UAC here is the MS explanation and work around. http://support.microsoft.com/kb/937624
Avatar of Todd710

ASKER

I am giving you credit for this question based on your answer the the issue was: "Perhaps by elevating the program you are causing it to run as a different user, presumably the Administrator".

However, the answer is that it is the same user different access tokens related to the UAC here is the MS explanation and work around. http://support.microsoft.com/kb/937624