Solved

setPropertyValue does not work with win32_volume?

Posted on 2011-03-08
6
637 Views
Last Modified: 2013-12-17
here is my code, basically, i want to assign drive letter "H“ to a removable usb drive
but when I try to set value for drive letter, there is no exception or error, nth happens.
problem seems to be following line, but i couldnt find out how to fix it.
mo.SetPropertyValue("DriveLetter","H:");

im using win7 and tested with administrator previllage when running debug.
can someone please help?
public static void Main(string[] args)
        {
            RetrieveDiskInfo_DriveLetter rdd = new RetrieveDiskInfo_DriveLetter();
  ManagementClass mc = new ManagementClass("Win32_Volume");
            ManagementObjectCollection moc =mc.GetInstances();
           
            if(moc.Count!=0)
            {  
           try{
           
            foreach (ManagementObject mo in moc)
                {
              
                    if (mo["DriveType"].ToString() == "2")
                       {
                            Console.WriteLine(mo["Drivetype"].ToString());
                            Console.WriteLine(mo["DeviceID"].ToString());
                                 mo.SetPropertyValue("DriveLetter","H:");
                       }
                    
                  }
             }
            catch (Exception ex)
               {
                            Console.WriteLine(ex.ToString());

               }
                
            
        }
      }

Open in new window

0
Comment
Question by:Ikelca
  • 4
6 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 35081216
Have you tried putting a slash at the end of the drive letter?

mo.SetPropertyValue("DriveLetter",@"H:\");

Open in new window

0
 

Author Comment

by:Ikelca
ID: 35082801
tried, it does not work
0
 

Author Comment

by:Ikelca
ID: 35082811
and no error msg, this is weird
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35084615
Call Put() to save changes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

class Program
{
	static void Main(string[] args)
	{
		ManagementClass mc = new ManagementClass("Win32_Volume");
		ManagementObjectCollection moc = mc.GetInstances();

		foreach (ManagementObject mo in moc)
		{
			if ((uint)mo["DriveType"] == 2)
			{
				try
				{
					mo["DriveLetter"] = "Z:";
					mo.Put();
				}
				catch (Exception ex)
				{
					Console.WriteLine("Error: {0}", ex.Message);
				}
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:Ikelca
ID: 35090948
ummm thanks for the reply, it works now on win7
but somehow it gets unhandled exception on WINXP
the debugger points to ManagementObjectCollection moc = mc.GetInstances();
do you know how come this happens??
0
 

Author Comment

by:Ikelca
ID: 35091072
nvm, i just found out win32_volume is not available to winxp and earlier windows.....
thanks guys
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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