I'm currently trying to do a basic mysqlcheck through the command prompt. The mysql.exe file itself is within a bin that the working directory is pointing towards to use as part of the commands though the cmd.
Right now I'm getting stuck to where it is stopping while opening the shell. It doesn't proceed to initiate the standardinput.
{
Process proc = new Process();
proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
proc.StartInfo.WorkingDirectory = #"C:\Program Files (x86)\MySQL\bin";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.Arguments = #"Mysqlcheck -u root -c -ppassword database";
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.StandardInput.WriteLine("Mysqlcheck -u root -c -ppassword database");
proc.WaitForExit();
}
Add the /C option before the name of the executable
proc.StartInfo.Arguments = #"/C Mysqlcheck.exe -u root -c -ppassword database";
Without this argument the cmd shell doesn't execute the command but exits immediately.
You can see other arguments for the CMD.exe opening a command prompt and typing CMD /?
Related
Hello I have some issue with cmd command via c#
for example I do manually commands in cmd.exe
//command1 to pick directory
cd C:\Users\NewSystem\source\repos
//command2 command to send file to emulator
adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download
code I test in C# is and it doesnt work properly, exactly not send nothing to android emulator
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WorkingDirectory = #"C:\Users\NewSystem\source\repos\";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = false;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
To start cmd.exe and have it execute a command you need to pass /c (execute command and exit) or /k (execute command and remain)...
cmd /c adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download
You don't need to have cmd.exe start adb.exe, though; just start adb.exe directly.
As for your C# code, StandardInput is for writing to a process's stdin stream, which is not what you want. The way to pass command line arguments is using the ArgumentList (.NET (Core) 2.1+) or Arguments (all .NET Framework/Core versions) properties of ProcessStartInfo...
// Dispose cmd when finished
using (Process cmd = new Process())
{
// The full executable path is required if its directory is not in %PATH%
cmd.StartInfo.FileName = #"C:\Users\NewSystem\source\repos\adb.exe";
cmd.StartInfo.Arguments = #"-s emulator-5554 push ""Debug\funnels\01\01.mp4"" ""/sdcard/Download""";
cmd.StartInfo.WorkingDirectory = #"C:\Users\NewSystem\source\repos\";
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.WaitForExit();
}
I set Arguments to a verbatim string with the last two arguments quoted just to show how you would do so if either path contain spaces.
The command line I need to execute is
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe" /Run "C:\unity\unity\MRTK Tutorial\Builds\MRTK Tutorial.sln"
This works from a windows command line without issues,
I formatted it into a string for visual studio
When running from C# this command never executes and the contents of result are ""
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("C:\\Program Files(x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\devenv.exe", " /Run \"C:\\unity\\unity\\MRTK Tutorial\\Builds\\MRTK Tutorial.sln\"");
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
You can try the following code to use c# to execute devenv.exe.
var devEnvPath = #"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe";
string SolutionFile = #"D:\Test\testconsole\testconsole.sln";
ProcessStartInfo startInfo = new ProcessStartInfo(devEnvPath);
startInfo.Arguments = "/Run " + SolutionFile;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Console.ReadKey();
Based on my test, the above code will open vs2019 and open the startup project.
Similar question was asked at least a dozen times on SO and it looks that now I have exhausted most of the proposed solutions but still unable to complete the task successfully.
So what I have is the following command that I want to run in cmd:
xcopy /q C:\fileName.txt \\VMNAME\C$\destFolder /Y /E
But I need it to be executed with certain credentials. So what I was doing manually is entering the below command first:
runas /user:<domainName>\<userName> cmd
That was opening a separate cmd window and I was running the first (xcopy) command in that window.
What I have at the moment:
string strCmdText = string.Format(#"xcopy /q {0} {1} /Y /E", source, destination);
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.FileName = "runas";
procStartInfo.Arguments = String.Format(#"/user:<domainName>\<userName> cmd " + strCmdText);
Process.Start(procStartInfo);
Where the source and destination are of the below structure:
source = "C:\\somePath\\fileName.txt"
destination = "\\\\<VMName>\\C$\\somePath\\"
I have also tried defining procStartInfo with:
procStartInfo.Verb = "runas";
Instead of:
procStartInfo.FileName = "runas";
With similar results.
At the moment, when I run the above code, it does not return any error but doesn't do what's expected either. Am I missing something or this approach is wrong?
I am using the following code to execute osql command and then get its output (like 2 rows affected etc) but it never finishes.
Please let me know what am I missing.
string sqlFilePath = Helper.GetFilePath(sqlFileName, Environment.CurrentDirectory);
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"osql -E -S #Server -d #Database -T -n -i ""#SqlFile"""
.Replace("#Server", ConfigurationManager.AppSettings["Server"])
.Replace("#Database", Path.GetFileNameWithoutExtension(DB))
.Replace("#SqlFile", sqlFilePath);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I believe you might be running into two separate problems:
Set the FileName and Arguments properties like this:
p.StartInfo.FileName = "osql.exe";
p.StartInfo.Arguments = #"-E -S #Server -d #Database -T -n -i ""#SqlFile"""
.Replace("#Server", ConfigurationManager.AppSettings["Server"])
.Replace("#Database", Path.GetFileNameWithoutExtension(DB))
.Replace("#SqlFile", sqlFilePath);
You might also encounter an encoding issue. make sure that you save you .sql file by using the Unicode Encoding (codepage 1200) (here's a question which describes the issue).
I want to run tabcmd.exe utility to publish views in tableau server. Manual step to do this is as follows, log file will be updated for every step in the location
"C:\Users[UserName]\AppData\Roaming\Tableau\tabcmd.log"
In the same session I have to perform this steps manually one by one order
Run cmd.exe
log in by using the command tabcmd login -s "http:/sales-server:8000" -t Sales -u administrator -p p#ssw0rd!
Create Project Name using the command tabcmd createproject -n "Quarterly_Reports" -d "Workbooks showing quarterly sales reports."
Publish views by using the command tabcmd publish "analysis.twbx" -n "Sales_Analysis" --db-user "jsmith" --db-password "p#ssw0rd"
Refresh by using the command tabcmd refreshextracts --workbook "My Workbook"
Log out by using the command tabcmd logout
Now I am trying to automate this steps from my .Net win form, so I used below code as a try and It is not working.
String path = #"C:\Program Files (x86)\Tableau\Tableau Server\7.0\bin\tabcmd.exe"
ProcessStartInfo startInfo = new ProcessStartInfo ();
startInfo.FileName = "\""+path+ "\"";
startInfo.Arguments = String.Format("login -s http://Server1:8000 --db-user "jsmith" --db-password "p#ssw0rd");
startInfo.UseShellExecute = false ;
startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("createproject -n \"MyProject\" -d \"MyProjectWorkbook\"");
//sw.WriteLine("My next Command");
//sw.WriteLine("My next Command");
}
}
I am able to log in successfully and I am not able to proceed consequent steps further, I have no clue how to proceed on this further, so I am looking forward some help on this.
Thanks in advance!
I'm unsure if this is of use to you or not but you could try creating a batch file with all of those commands and the execute the batch file from command prompt like this :-
//Build Commands - You may have to play with the syntax
string[] cmdBuilder = new string[5]
{
#"tabcmd login -s 'http:/sales-server:8000' -t Sales -u administrator -p p#ssw0rd!",
#"tabcmd createproject -n 'Quarterly_Reports' -d 'Workbooks showing quarterly sales reports.'",
#"abcmd publish 'analysis.twbx' -n 'Sales_Analysis' --db-user 'jsmith' --db-password 'p#ssw0rd'",
#"tabcmd refreshextracts workbook 'My Workbook'",
#"tabcmd logout"
};
//Create a File Path
string BatFile = #"\YourLocation\tabcmd.bat";
//Create Stream to write to file.
StreamWriter sWriter = new StreamWriter(BatFile);
foreach (string cmd in cmdBuilder)
{
sWriter.WriteLine(cmd);
}
sWriter.Close();
//Run your Batch File & Remove it when finished.
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\cmd.exe";
p.StartInfo.Arguments = #"\YourLocation\tabcmd.bat";
p.Start();
p.WaitForExit();
File.Delete(#"\YourLocation\tabcmd.bat")
I'll leave the output section to yourself. You could try this, it is not exactly clean but will automate the main steps. It's either this, or opening a process for each command as the last process exits?
Hope this is of help.