Solved

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

Posted on 2011-09-21
6
941 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:Todd710
  • 4
6 Comments
 
LVL 40
ID: 36576271
Mapped drive do not have 2 slashes in their path. Replace ("P:\\") by ("P:\")
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 36576378
@JamesBurger

A back-slash in C# has special meaning, and in order to include a back-slash in a string literal it must be escaped by preceding it with another back-slash. Using P:\ is invalid, and would produce a compilation error.


@Todd710

Perhaps by elevating the program you are causing it to run as a different user, presumably the Administrator, and if that user doesn't have P:\ mapped your results would be empty.

Also, look into System.IO.Drives.GetDrives() - that will give you some information about network drives, and would be preferable to WMI (assuming it includes all the information you need).  http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx
0
 

Author Comment

by:Todd710
ID: 36580375
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.  
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:Todd710
ID: 36580522
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
0
 

Author Comment

by:Todd710
ID: 36580589
@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
0
 

Author Closing Comment

by:Todd710
ID: 36580603
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
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question