Solved

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

Posted on 2011-09-21
6
973 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
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 a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

756 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