1

I have below code to format a USB drive. Code works fine with Admin account, but if I run the exe using Non Admin account, it returns 3 (Access Denied).

I want to format a drive in Non-Admin mode. Any help? I visited this link https://social.msdn.microsoft.com/Forums/en-US/1e192745-9d58-4507-93f0-ceacbc0cde96/wmi-win32volume-format-method-returns-access-denied?forum=windowsgeneraldevelopmentissues , but no help

ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
foreach (ManagementObject vi in searcher.Get())
{
   var result = vi.InvokeMethod("Format", new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });
   if (Convert.ToInt32(result) != 0)
   {
      throw new Exception("Error while formating drive");
   }
}
vishal
  • 11
  • 1

1 Answers1

0

Have you tried "Right Click> Compatibility> Change All User Settings> Run As Administrator"? If this is the solution, you can do this with the code.

Probably, this question - answer, can answer your problem. How do I force my .NET application to run as administrator?

  • No I don't want to run my application as Administrator. It should be run on Normal User account only. – vishal Apr 18 '19 at 08:42