Solved

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

Posted on 2011-09-21
6
956 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

837 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