Solved

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

Posted on 2011-09-21
6
922 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Todd710
Comment Utility
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
Comment Utility
@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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
OfficeMate Freezes on login or does not load after login credentials are input.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now