0

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
});

`

Lesnik
  • 1
  • 2
  • Please provide more detailed information. Where is your usage of Subst? What is the exact error? – Martin Meeser Dec 22 '22 at 09:30
  • Does this answer your question? [creating virtual hard Drive](https://stackoverflow.com/questions/3753758/creating-virtual-hard-drive) – Klaus Gütter Dec 22 '22 at 09:36
  • Windows 10. Hands through cmd subst turns out, the disk is created. And C# doesn't work. Tried both console application and windows forms – Lesnik Dec 22 '22 at 09:49
  • Does this answer your question? creating virtual hard Drive – Klaus Gütter Thank you. I also found this and tried it myself. – Lesnik Dec 22 '22 at 09:50
  • There is no exception, just nothing happens and that's it. – Lesnik Dec 22 '22 at 09:50

1 Answers1

0

First of all I would recommend you to use CliWrap libriary, that will improve your work with cli in .NET.

I think, you should check this question to understand how to use cli (cmd) wrapping in .NET.

So let's try to remake answer in this question to help you.

Don't forget that subst is command that requires the admin privileges, so.

string driveLetter = "P";
string path = "C:\\my\\folder\\path";

Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C copy subst {driveLetter}: {path}";
startInfo.Verb = "runas";

process.StartInfo = startInfo;
process.Start();
  • Jeroen Mostert's solution worked immediately. That's why I marked it as a solution. Thank you too! I'll check out everything you suggested. – Lesnik Dec 22 '22 at 10:01
  • @Lesnik: I can't take credit; I think you'll need to praise the creators of ChatGPT, as the answer has all the hallmarks of being generated by it. – Jeroen Mostert Dec 22 '22 at 10:09
  • @Jeroen Mostert: Sorry, I don't know what ChatGPT is. Googled, found out, but did not use. I would be glad to thank everyone who helped, but I still have a small rating to rate. Alas. Or I don't understand what it is. Sorry. – Lesnik Dec 22 '22 at 10:20