Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Create virtual disk with Subst command

I need to create a directory with the letter P (or whatever) so that it is like drive C or D.
After trying different methods, I came to the conclusion that it is best to do this with the help of Subst.
But how to do it in C#. The code I’m using doesn’t work. As administrator, does not work either.

`

string diskLetter = "P:"; // <---
string path = @"D:\folder path";
 
Process.Start(new ProcessStartInfo
{
    FileName = "subst",
    Arguments = $"{diskLetter} \"{path}\"",
    Verb = "runas",
    UseShellExecute = true
});

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To use the subst command in C#, you can use the Process class to start a new process and run the command. Here is an example of how you can do this:

using System.Diagnostics;
// ...
string driveLetter = "P";
string path = "C:\\my\\folder\\path";

Process process = new Process();
process.StartInfo.FileName = "subst";
process.StartInfo.Arguments = $"{driveLetter}: {path}";
process.Start();

This will create a virtual drive with the letter P that maps to the folder at the specified path.

Note that you will need to have administrative privileges to create a virtual drive using subst. If the code is not working, it could be because the process is not being started with the necessary privileges. To start the process with administrative privileges, you can set the Verb property of the StartInfo object to "runas":

process.StartInfo.Verb = "runas";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading