I am trying to execute 'move' command, how you would in the command prompt, but it fails using Process.Start(). I understand move seems to be some integrated part of cmd and not an isolated executable, is there any way to trigger it using Process anyway ? I need to move some special location folder and trying to use elevated process instead of Directory.Move.
Asked
Active
Viewed 338 times
-2
-
`cmd /C
`? I.e. invoke `cmd.exe`, and tell it to run its `move` command – canton7 Apr 29 '21 at 10:48 -
Is elevation not available to your app so you can do it via C#? – Caius Jard Apr 29 '21 at 10:50
-
I tried invoking cmd and tell it to run move but it does not work – frno Apr 29 '21 at 11:10
-
its a net core service which tries to migrate some data, it can run process in elevated mode but it fails to Directory.Move – frno Apr 29 '21 at 11:11
-
Does Directory.Move fails with UnauthorizedAccessException? – gigiabbrescia Apr 29 '21 at 11:30
-
2@frno What exactly did you try, and what makes you think it doesn't work? I've definitely called into `cmd` to do similar things in the past – canton7 Apr 29 '21 at 12:06
1 Answers
1
That should work. The arguments should start with '/C' otherwise won't work.
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = @"/C move ""test.txt"" ""test/test.txt""",
UseShellExecute = false
}
};
process.Start();

crankedrelic
- 463
- 1
- 5
- 14
-
1(Just as a note: the default for `UseShellExecute` changed from `true` to `false` with the introduction of .NET Core. Annoyingly, therefore, portable code which runs on both Framework and Core needs to always explicitly set it, as this answer does) – canton7 Apr 29 '21 at 15:18