Solved

setPropertyValue does not work with win32_volume?

Posted on 2011-03-08
6
639 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
Independent Software Vendors: 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!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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