rebuild wmi classes with c# - c#

i am writing c# code to rebuild the wmi classes. i found that it can be done with the help of these command as discussed here :
Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll
cd /d %windir%\system32\wbem
for %i in (*.dll) do RegSvr32 -s %i
for %i in (*.exe) do %i /RegServer
i have tried passing these command in cmd and reading the output as :
Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
//objProcess.StartInfo.CreateNoWindow = true;
//objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string cmd1=#"Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll";
string cmd2=#"cd /d %windir%\system32\wbem";
string cmd3=#"for %i in (*.dll) do RegSvr32 -s %i";
string cmd4 = #"for %i in (*.exe) do %i /RegServer";
string strFinal = cmd1 + "\n" + cmd2 + "\n" + cmd3 + "\n" + cmd4;
//string cmd1=#"Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll cd /d %windir%\system32\wbem for %i in (*.dll) do RegSvr32 -s %i for %i in (*.exe) do %i /RegServer";
objProcess.StartInfo.FileName = string.Format("cmd.exe");
objProcess.StartInfo.Arguments = string.Format(strFinal);
try
{
objProcess.Start();
}
catch
{
throw new Exception("Error");
}
StreamReader strmReader = objProcess.StandardOutput;
string strTempRow = string.Empty;
while ((strTempRow = strmReader.ReadLine()) != null)
{
Console.WriteLine(strTempRow);
}
if (!objProcess.HasExited)
{
objProcess.Kill();
}
Can one share idea how to achieve this by code not manually.

I assume you have problems executing ms-dos commands, not reading the output.
Have you tried creating a batch file with those commands and executing it with system process class?
First of all, create the batch file with those commands and execute it manually. If it's works fine then just execute it as you done before.
System.Diagnostics.Process.Start("wmiBuilder.bat");

Related

The system cannot find the file specified while running command through Process module in C#

I am trying to execute one command through process but it is throwing exception as "The system cannot find the file specified". When i run this command directly on command prompt. It is working fine.
Command: start cmd.exe #cmd /k "NTttcpr.exe -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500-Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log"
This command executes perfectly if i run on command prompt.
This is how i written code:
string tool = #"NTttcpr.exe";
string command = " -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500-Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log";
private void RunCommand(string tool, string command)
{
try
{
logger.Info($"{MethodBase.GetCurrentMethod()}: {tool} {command}");
Process pro = new Process();
pro.StartInfo.FileName = "start cmd ";
pro.StartInfo.Arguments = "#cmd /k " + '"' + tool + " " + command + '"';
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
logger.Info($"{MethodBase.GetCurrentMethod()}: Executing command: {tool} {command}");
pro.StartInfo.Verb = "runas";
pro.Start();
//pro.WaitForExit(MillisecondsTimeout);
//Thread.Sleep(MillisecondsTimeout);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
logger.Error($"{MethodBase.GetCurrentMethod()}: Exception occurred while uni-directional command!!");
logger.Error($"{MethodBase.GetCurrentMethod()}: {ex}");
}
}
Note:
NTttcpr.exe file is already present in current executing directory.
Please help me to solve this.
This should be because you have not set the working directory, add pro.StartInfo.WorkingDirectory = "path to NTttcpr.exe" do not add NTttcpr.exe, just add the location.
Let me know if this works.
cmd.exe is not required for Process class. Try like below.
pro.StartInfo.FileName = "NTttcpr.exe";
pro.StartInfo.Arguments = command

How use cmd command in c# program?

I want cmd command in my c# winforms program. I want use command to compress file with 7zip.
In normal cmd i use cd "C:\Program Files(x86)\7-Zip" & 7z.exe a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
string zip = #"/c C:\Program Files(x86)\7-Zip";
string file = #"/c C:\xyz\txt";
string conv = "/c & 7z.exe a -tzip";
string method = "/c -m0=BZip2 -mx5";
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = #"C:\windows\system32\cmd.exe";
proc.Arguments = #"/c cd" + zip + conv + file + file + method;
System.Diagnostics.Process.Start(proc);
Doesn't work my code. How can i use this command in my program. I want compress file when i click in button
Something like this:
// File (exe) to start: combination of folder and exe
string fileName = Path.Combine(
// Let's not hardcode "C:\Program Files(x86)" and alike
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
#"7-Zip",
#"7z.exe");
// If desired arguments are
// a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
// we can join them as
string arguments = string.Join(" ",
#"a",
#"-tzip", //TODO: you may want to have these values
#"""C:\xyz\test\txt""", // as variables like file, conv, method etc.
#"""C:\xyz\test\txt""",
#"-m0=BZip2",
#"-mx5");
ProcessStartInfo procInfo = new ProcessStartInfo() {
FileName = fileName, // We want to start fileName
Arguments = arguments, // With arguments
}
// Process is IDisposable, do not forget to Dispose HProcess handle
using (var process = Process.Start(procInfo)) {
// It's fire and forget implementation, if you want to wait for results
// add process.WaitForExit();
// process.WaitForExit(); // uncomment, if you want to pause
}
You seem to have made a mistake with your arguments.
Your current command line will be:
C:\windows\system32\cmd.exe /c cd/c C:\Program Files(x86)\7-Zip/c & 7z.exe a -tzip/c C:\xyz\txt/c C:\xyz\txt/c -m0=BZip2 -mx5
The spurious /c everywhere is not going to help and neither are the lack of spaces.
I'm guessing what you meant to run was these two commands in succession:
cd C:\Program Files(x86)\7-Zip
7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
Which means you should change your code to:
string zip = #" C:\Program Files(x86)\7-Zip";
string file = #" C:\xyz\txt";
string conv = " & 7z.exe a -tzip";
string method = " -m0=BZip2 -mx5";
This will produce:
C:\windows\system32\cmd.exe /c cd C:\Program Files(x86)\7-Zip & 7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5

Remove directory after copy it XCOPY

I'm working with C#, using XCOPY. I have method which copies a full directory into another:
public static void ProcessXcopy(string SolutionDirectory, string TargetDirectory)
{
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
//Give the name as Xcopy
startInfo.FileName = "xcopy";
//make the window Hidden
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//Send the Source and destination as Arguments to the process
startInfo.Arguments = "\"" + SolutionDirectory + "\"" + " " + "\"" + TargetDirectory + "\"" + #" /e /y /I /B";
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception exp)
{
throw exp;
}
}
That I want to know if there is a way to remove source directory once it is copied to another directory successfully.
If you want to stick .Net Methods, you can use Directory.Delete in the finally statement . The second parameter indicates to remove the Sub Folder/Files. More details here
Directory.Delete(path,true);
you can use robocopy instead of xcopy
robocopy from_folder to_folder files_to_copy /MOVE
xcopy needs .bat script in order to have same functionality with 1 line of robocopy
Ex:
xcopy /D /V %1 %2
if errorlevel 0 (
del /Q %1
exit /B
)

How to escape characters in string variables? (cmd commands)

I have a list of cmd commands I need to run:
sc config winmgmt start= demand
net stop winmgmt /y
rd /S /Q %windir%\\system32\\wbem\\repository
rd /S /Q %windir%\\syswow64\\wbem\\repository
C:\\WINDOWS\\system32\\msiexec.exe /unregserver
C:\\WINDOWS\\system32\\msiexec.exe /regserver
regsvr32 /s C:\\WINDOWS\\system32\\scecli.dll
regsvr32 /s C:\\WINDOWS\\syswow64\\scecli.dll
for %i in (C:\\WINDOWS\\system32\\wbem\\*.dll) do RegSvr32 -s %i
mofcomp C:\\WINDOWS\\system32\\wbem\\rsop.mof
mofcomp C:\\WINDOWS\\system32\\wbem\\en-US\\rsop.mfl
for %i in (C:\\WINDOWS\\syswow64\\wbem\\*.mof) do mofcomp %i
mofcomp.exe C:\\Windows\\system32\\WBEM\\win32_encryptablevolume.mof
sc config winmgmt start= auto
net start winmgmt
I have a txt file in Properties.Resources containing the above commands. And I try to run them like this:
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
if (process != null)
{
foreach(var myString in File.ReadAllLines(Properties.Resources.commandsTXT))
{
process.StandardInput.WriteLine(myString);
}
process.StandardInput.Close();
}
The problem is when I try to run it, I get Unhandled Exception: System.ArgumentException: Illegal characters in path. How could I possibly escape every character in it? As you can see I already tried replacing \s to \\s, but no luck

Execute CMD Command by C# winform app?

I have sample code to run command but its not working ( just opens CMD ) without executing the command
string strCmdLine =
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
where is problem ?
You need to add a /C
Correct syntax for CMD.exe is
CMD.EXE /c command
string strCmdLine =
"/C C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
You don't need to use cmd.exe mate...
I guess this should do the job for you...
string strCmdLine =
"\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"";
var parmaters = "google.com";
System.Diagnostics.Process.Start(strCmdLine, parmaters);
you need the parameter "/c"
string strCmdLine =
"/c C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();

Categories

Resources