Deleting directory that contains items - c#

I am trying to delete a directory using C#. The first method I tried was
Directory.Delete(#"C:\Program Files (x86)\Qmuzki32");
I get an exception stating that the directory is not empty. I then found a cmd command which I can use to delete the directory quietly regardless of the fact that the directory is empty or not. I ran the following command in cmd:
rmdir /s /q "C:/Program Files (x86)/Qmuzik32"
This worked and did exactly what I wanted it to do. With my first attempt I tried building this command into a C# process like so:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "cmd.exe";
del.StartInfo.Arguments = string.Format("rmdir /s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
This did not work and then I tried it like this:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "rmdir.exe";
del.StartInfo.Arguments = string.Format("/s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
Same problem. I get the exception:
No process is associated with this object.
I do think I am on the right track; maybe the code above just requires some tweaking.

Just use Directory.Delete(string, bool).
While the low-level filesystem APIs of course require you to make sure the directory is empty first, any half-decent framework abstracting them allows you do do a recursive delete. In fact, existence of such a method would be the first thing I'd check before even trying to resort to external programs.

If you want to use the cmd way you can use this:
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C rd /s /q \"C:\\Program Files (x86)\\Qmuzik32\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);

del.Start();
del.WaitForExit();
you didn't start the procces so it doesn't have a PID so it dies

Related

Why is this Command Prompt Script not running in C#?

I need to run a bunch of commands in C# in order for my program to run correctly. Previously, this script ran flawlessly and I used to get the desired output my program needs in order to compile and run correctly.
This is the script I have currently
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = #"/C mkdir C:\IL & del Output.il & cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools & ildasm /out=C:\IL\Output.il " + filePath;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
Filepath is a parameter I pass back to this method. This parameter contains the file's URL.
When the filepath is loaded into the application now, this set of commands does not run whatsoever. There is no output file or anything. However, these commands run when I enter them manually into Command Prompt.
I've tried this process from this answer, and implemented the following code to try and get this batch file to work.
ProcessStartInfo cmd = new ProcessStartInfo( "cmd", #"/C mkdir C:\IL & del Output.il & cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools & ildasm /out=C:\IL\Output.il" + filePath );
cmd.CreateNoWindow = true;
cmd.UseShellExecute = false;
Process.Start( cmd );
The above method hasn't worked out, and the other posts on here have the same code as my own script, which doesn't help at all.
Is there anything wrong with my script? Or is there a setting that I can enable in Visual Studio?
Thanks for viewing

The system cannot find the file specified but works when direct in command line

I am struggling to use Chromes print to PDF feature via headless browsing.
My code is very simple
var command = "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage";
Process.Start(command);
When I view my command and copy the string, and paste that into a command prompt, it works fine.
This is all on one system so I don't understand why it works in command prompt and not in my C# web app.
Try this
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = #"C:\Program Files(x86)\Google\Chrome\Application\chrome.exe";
proc.Arguments = #"--headless --disable-gpu --print-to-pdf=\""D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\"" http://localhost/mypage";
Process.Start(proc);
The system is now searching for the file "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage", but you want it to launch "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe" with some arguments. What you need to do is:
Process process = new Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = "arguments";
process.Start();

Run type as process in c#

I can join videos in windows at command line with
type vid1.avi vid2.avi > vidjoined.avi
I try to run this in c#:
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = "cmd.exe";
cmdStartInfo.Arguments = "type vid1.avi vid2.avi > vidjoined.avi";
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.Start();
cmdProcess.WaitForExit(120000);
What is wrong with my code?
It runs forever and I get no console output.
I'm not sure it's valid to concatenate two AVI files together, but that aside:
Try changing your arguments to:
cmdStartInfo.Arguments = "/c type vid1.avi vid2.avi > vidjoined.avi"
The "/c" will cause the command shell to execute and then exit.
Also, the working directory is going to be wherever cmd.exe is. Your relative paths to your files will likely not resolve properly. Either set cmdStartInfo.WorkingDirectory to the directory that your files are in, or use fully qualified paths in your arguments.

Compile an .asm file through c# using cmd

I want to compile an .asm file placed in bin folder in the masm through c#.I have tried multiple methods like process.start but nothing helps.It opens the cmd but the command "ml" never executes.It either open the pwb.exe(MASM) or the 'file.asm' in notepad.I give these arguments to CMD "path\ml file.asm" which works fine manually.ml is a command used to compile .asm files. One of the method I used is following
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\WINDOWS\system32\cmd.exe";
startInfo.Arguments = "C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml file.asm";
process.StartInfo = startInfo;
process.Start();
If you want to start the process in this way, you'll need to put quotes round the path, due to the spaces:
startInfo.Arguments = #"""C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml"" file.asm";
(In a verbatim string literal, you include double-quotes by doubling them.)
Alternatively, if ml is actually an executable (I know nothing about masm) you could just use:
startInfo.FileName = #"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe";
startInfo.Arguments = "file.asm";
To start "cmd.exe" and run another program you need to use the /c switch.
/C Carries out the command specified by string and then terminates
So you would need:
startInfo.Arguments = "/c \"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe\" file.asm";
I've added extra quotes for the space in the path.
If you don't need to run it via the command prompt - "cmd.exe" - then you can put the path to "ml.exe" in FileName and just pass the "file.ml" as the Arguments.

problems with stdout and psexec.exe from sysinternals

i have searched and read about issues with psexec.exe from sysinternals not working properly with c# and stdout. i am now trying to figure out how to just call a batch file that has the following instead of using System.Diagnostics.Process to call psexec:
test.bat contains the following line:
psexec.exe \\hostname -u user -p password ipconfig /all >c:\test.txt
test.txt will be saved on the host where i am running my c sharp app and executing psexec.
when i execute the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process();
psexec_run.StartInfo.FileName = "cmd.exe";
psexec_run.StartInfo.Arguments = #"/c """ + cur_dir + #"\test\test.bat""";
psexec_run.StartInfo.UseShellExecute = false;
psexec_run.Start();
psexec_run.WaitForExit();
i see the cmd window pop up and it runs something but not sure what and goes away.
if i execute the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process();
psexec_run.StartInfo.FileName = cur_dir + "\\test\\psexec.exe";
psexec_run.StartInfo.Arguments = #"\\hostname -u user -p password ipconfig /all";
psexec_run.StartInfo.UseShellExecute = false;
psexec_run.Start();
psexec_run.WaitForExit();
then i see the command window open and it runs psexec which takes quite a few secs and i quickly see my output i need, but i have no way of capturing the output or writing it to a file.
i guess my issue now is since psexec will not work with stdout how can i capture the output from the psexec command to write it to a file???
see the following link for the issues with psexec, the last reply on this url mentioned a way to write the process output to a file without using stdout, i'm newbie to c# i can't figure out how to write process output without use stdout :(
http://forum.sysinternals.com/psexec-fails-to-capture-stdout-when-launched-in-c_topic19333.html
based on response below i tried the following:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + "\\test\\psexec.exe", #"\\hostname -u user -p password ipconfig /all");
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader myStreamReader = p.StandardOutput;
// Read the standard output of the spawned process.
string sOutput = myStreamReader.ReadToEnd();
i did ReadToEnd so i would make sure it got all the output, it DID NOT!! for some reason it only go the first line of ipconfig output that was it. Also the cmd window it opened up never closed for some reason. even with CreateNoWindow=true the code just hangs. so again something is wrong with psexec and stdout i think?? as i can run this code just fine using ipconfig /all command on the local host and not use psexec...
again i am looking to avoid stdout and somehow find a way to get the output from this command or unless there is something else i'm over looking? also, not to make more work for anyone, but if you d/l psexec.exe from sysinternals and test it with a command on a remote host you will see. i have spent 2 days on this one :( trying to figure out how to use psexec or find some other quick method to execute remote command on a host and get ouptput.
UPDATE:
i gave up on psexec in c# code, i saw many posts about psexec eating the output, having a child window process ,etcc
until my head hurt :) so i am trying to run a batch file and output to a file and it's not making sense...
i have a batch file test.bat with the following
psexec.exe \\\hostname -u name -p password ipconfig /all >c:\test.txt
when i run the following code:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + #"\test\test.bat");
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
p.WaitForExit();
the cmd windows comes and goes really quickly and the test.txt file is created but is 0 bytes no info in it.
so if i run the batch file in a windows cmd line with the psexec command it works perfectly fine!!???
so then to verify psexec was the issue i changed the batch file to:
ipconfig /all >c:\test.txt
i execute my code above and it works fine creates the output in the test.txt file..???!!!!
why is not working with psexec am i missing something? if it's psexec, does anyone have
any recommendations for how i can execute a command on a remote windows host and get me the
output???
I have an answer to this problem that has worked for me.
Hopefully someone else will find it useful.
I have literally just spent the last two hours tearing my hair out with this. The psexec tool runs completely fine using a normal command prompt but when attempting to redirect the streams it truncates the output and you only get half output back.
In the end how I fixed my issue was a little bit of a hack. I piped the output of the command to a text file and read it back in to return it from the function.
I also has to set UseShellExecute to true. Without this it still wouldn't work. This had the unfortunate side effect of showing the console window. To get around that I set the window style to be hidden and hey presto it works!!!
Heres my code:
string ExecutePSExec(string command)
{
string result = "";
try
{
string location = AppDomain.CurrentDomain.BaseDirectory;
// append output to file at the end of this string:
string cmdWithFileOutput = string.Format("{0} >{1}temp.log 2>&1", command,location );
// The flag /c tells "cmd" to execute what follows and exit
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmdWithFileOutput);
procStartInfo.UseShellExecute = true; // have to set shell execute to true to
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden; // as a window will be created set the window style to be hiddem
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
// now read file back.
string filePath = string.Format("{0}temp.log", location);
result = System.IO.File.ReadAllText(filePath);
}
catch (Exception objException)
{
// Log the exception
}
return result;
}
and its usage:
string command = #"psexec.exe -l -u domain\username -p password /accepteula \\192.168.1.3 netstat -a -n";
ExecutePSExec(command);
I had exactly same problem. i was getting "Windows ip config. " as first line when i run with psexec. i tried with paexec it worked well. I used Marius's code.
Note: if you dont use first cmd / c in arguments command runs only on local computer even if you define target as \\remoteserver
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.Arguments = #"cmd/c C:\paexec.exe \\\192.168.2.5 -s -u test.local\administrator -p Password1 cmd /c ipconfig";
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
System.IO.StreamReader myStreamReader1 = p.StandardOutput;
p.WaitForExit();
string sOutput = myStreamReader1.ReadToEnd();
Are you sure your sourcecode is correct? that link is quite a bit old.. maybe its fixed!
Heres an example how to redirect the standard-output and put whole output in a string via streamreader:
ProcessStartInfo psi = new ProcessStartInfo("tftp.exe");
// preferences for tftp process
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader myStreamReader = p.StandardOutput;
p.WaitForExit();
// Read the standard output of the spawned process.
string sOutput = myStreamReader.ReadToEnd();
i found a solution. apparently psexec is NOT going to work in c sharp. so i came up with some wmi code to connect to a remote host and it's working PERFECTLY!!! :)
i used microsoft's WMICodeCreator.exe to create wmi code for C# for the process method on a remote host, wow that tool is amazing because wmi code is little confusing to me.
psexec's output goes to StandardError and not StandardOutput. I don't know why it is that way. Following code snippet access it.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
errors = process.StandardError.ReadToEnd();
process.Close();

Categories