4

I want to change a drive letter. For example, I can use diskpart to assign a new letter to a drive (a USB disk or a new hard disk).

How can I implement it in C/C++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mark
  • 292
  • 1
  • 6
  • 18
  • I came across [this article](http://msdn.microsoft.com/en-us/library/aa364014.aspx) on msdn. Maybe it helps someone (it was exactly what I was looking for). –  Nov 14 '10 at 03:37
  • Check the following links: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364014%28v=vs.85%29.aspx (This one is an example with source code, very useful for what you want to do) And the reference to "DefineDosDevice" function: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363904%28v=vs.85%29.aspx Hope this helps – MrTheV Jan 09 '13 at 22:32

2 Answers2

2

SetVolumeMountPoint(L"X:\", volumeName.c_str())

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • From the docs "... a directory on another volume (for example, Y:\MountX). The string must end with a trailing backslash ('\')." -- thats a great *bad* example. – Ruddy Jan 21 '10 at 12:23
  • Yeah, luckily that example is irrelevant in this case. – MSalters Jan 21 '10 at 15:15
1

A trivial and easy way to do this would be to just shell out to diskpart:

int main () {
  int i = system("diskpart ..."); // Add args here.
  cout << "command exited with code: " << i;
  // ...
}

It has an /s parameter that you can use to supply a script to run inside diskpart, so you can simply write a text file out with the relevant subcommands and pass that into diskpart with your system(...) call.

John Feminella
  • 303,634
  • 46
  • 339
  • 357