Doxygen provides a way to pass in the contents of the .doxy file through stdin rather than passing a file name, but I don't know how to do it from C#.
For simplicity let's say the contents of my doxygen config file are simply stored in string[] lines so I want to execute doxygen.exe and feed this content in.
I got this working myself from the links mentioned in the comments, something along the lines of:
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = " -";
// Enter the executable to run, including the complete path
start.FileName = "doxygen.exe";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = false;
start.RedirectStandardInput = true;
start.UseShellExecute = false;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
//doxygenProperties is just a dictionary
foreach (string key in doxygenProperties.Keys)
proc.StandardInput.WriteLine(key+" = "+doxygenProperties[key]);
proc.StandardInput.Close();
proc.WaitForExit();
// Retrieve the app's exit code
int exitCode = proc.ExitCode;
}
Related
I want to run several commands via C# application like
Previously I had a batch file and I ran it using C# but now few of the commands can take inputs. But how to do that ?
I tried
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
Environment.SetEnvironmentVariable("filename", FileName);
startinfo.FileName = #"C:\Users\cnandy\Desktop\St\2nd Sep\New_CN\New folder\Encrypt web.config_RSAWebFarm\Decrypt Connection String.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
And in the batch file
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
aspnet_regiis -pi "CustomKeys2" "C:\Users\cnandy\Desktop\Encryption_keys\CustomEncryptionKeys.xml"
aspnet_regiis -pa "CustomKeys2" "NT AUTHORITY\NETWORK SERVICE"
aspnet_regiis -pdf "connectionStrings" %filename%
But effectively they did not run get executed at all. How to achieve the same where for the last command I can accept an input instead of hard coding
"C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"
?
Try this:
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
using (StreamWriter sw = p.StandardInput)
{
sw.WriteLine("First command here");
sw.WriteLine("Second command here");
}
p.StandardInput.WriteLine("exit");
Alternatively, try this more direct way (which also implements the last thing you requested):
string strEntry = ""; // Let the user assign to this string, for example like "C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
p.StandardInput.WriteLine("cd C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319");
p.StandardInput.WriteLine("aspnet_regiis -pi \"CustomKeys2\" \"C:\\Users\\cnandy\\Desktop\\Encryption_keys\\CustomEncryptionKeys.xml\"");
p.StandardInput.WriteLine("aspnet_regiis -pa \"CustomKeys2\" \"NT AUTHORITY\\NETWORK SERVICE\"");
p.StandardInput.WriteLine("aspnet_regiis -pdf \"connectionStrings\" " + strEntry);
p.StandardInput.WriteLine("exit"); //or even p.Close();
If using the second way, it is recommended to let the user enter the path in a textbox, then grab the string from the textbox's Text property, where all the necessary extra back-slashes will be automatically appended.
It should be mentioned that no cmd will show up running your batch file, or the commands.
You can still make a batch file as you used to. Only change it needs is accepting variables.
Something like
CallYourBatch.bat "MyFileName"
Then in you batch file, you can accept a parameter
SET fileName=%~1
aspnet_regiis -pdf "connectionStrings" %fileName%
Similarly the same functionality can be used while forming your command text, if you must do it as part of C# code.
Also might i suggest using a CALL command to call your batch files? More information on the same is at http://ss64.com/nt/call.html
Start the batch file using Process object. You can use Environment variables to pass the values between processes. in this case, from C# to bat file you can pass values using environment variable.
c#
Environment.SetEnvironmentVariable("searchString","*.txt")
in bat file you can access the value as like below
dir %searchString%
To start the bat file from c#
System.Diagnostics.Process.Start("path\commands.bat");
Sample code to start notepad from C# with Batch file and Variables
System.Environment.SetEnvironmentVariable("fileName", "test.txt");
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/commands.bat");
Console.ReadLine();
Bat file
#echo "staring note pad"
notepad %fileName%
I'm trying to execute TFS via Process.Start but am having some difficulty and I can't understand why. Here's my code snippet:
/// <summary>
/// Get the entire history for a project
/// </summary>
public void GetHistory(String project)
{
ProcessStartInfo info = new ProcessStartInfo();
String fileName = Path.GetTempFileName();
info.Arguments = String.Format("history \"{0}\" /recursive /format:Detailed /noprompt > {1}", "c:\\source\\ " + project, fileName);
info.FileName = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\tf.exe\"";
info.RedirectStandardError = true;
info.UseShellExecute = false;
Process process = new Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardError.ReadToEnd());
Console.WriteLine("History written to " + fileName);
Console.ReadKey();
}
This is resulting in a set of arguments like so (I've just removed the full project name):
I then get the following error:
The history command takes exactly one item.
If I piece the string together and execute in a normal command line however then it works:
Can anyone tell me what I'm missing?
You can't redirect output to a file in the Process.Start arguments. File redirection is a function of the shell.
If you want to put the history into a file, you will need to File.Open the file yourself, read the output of the tf history command and write it to the file.
Or you could use a command script or PowerShell script.
I have tried to redirect the command prompt output to a file using Asp.Net C#.
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "c:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = #"/c dir" +">" + #"Myval.txt";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
Response.Write(output);
si.Close();
The file is getting created successfully but no content present in it.
Even the variable Output returns nothing.
Help me to resolve this issue.
EDIT after being corrected:
I just tested on my machine and the code works perfectly. I apologize for not reading and testing carefully myself. Myval.txt is created and the DIR output is written into it.
The output variable is empty because you are rerouting any output by the DIR command into the txt file, so that's by design.
Please see if there are any locks on the txt file preventing it from being overwritten. Further than that, I can only guess that there is a security issue preventing the DIR command from running.
IIS7 - I tested this various ways including using a Batch file but the application isn't available on desktop. I can see the worker process and the exe running under my user name but with session id value of zero.
The following has worked for me through command prompt:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I want to run an exe file by my c# code. The exe file is a console application written in c#.
The console application performs some actions which includes writing content in database and writing some files to directory.
The console application (exe file) expects some inputs from user.
Like it first asks , 'Do you want to reset database ?' y for yes and n for no.
again if user makes a choice then application again asks , 'do you want to reset files ?'
y for yes and n for no.
If user makes some choice the console application starts to get executed.
Now I want to run this exe console application by my c# code. I am trying like this
string strExePath = "exe path";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = strExePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
I want to know how can I provide user inputs to the console application by my c# code?
Please help me out in this. Thanks in advance.
You can redirect input and output streams from your exe file.
See redirectstandardoutput
and redirectstandardinput for examples.
For reading:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
For writing:
...
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("y");
...
myStreamWriter.Close();
ProcessStartInfo has a constructor that you can pass arguments to:
public ProcessStartInfo(string fileName, string arguments);
Alternatively, you can set it on it's property:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";
Here is a sample of how to pass arguments to the *.exe file:
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = #"\filepath.exe";
p.StartInfo.Arguments = "{insert arguments here}";
p.Start();
error += (p.StandardError.ReadToEnd());
p.WaitForExit();
I try to start ilasm from C# using class ProcessInfo
string arguments = string.Format("\"{0}\" /exe /output:\"{1}\" /debug=IMPL", ilFullFileName, exeFileFullName);
ProcessStartInfo processStartInfo = new ProcessStartInfo(CILCompiler, arguments);
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = false;
processStartInfo.WorkingDirectory = #"c:\Windows\Microsoft.NET\Framework\v4.0.30319\";
using (Process process = Process.Start(processStartInfo))
{
process.WaitForExit();
}
the arguments are:
"path_to_il.il" /exe /output:"path_to_exe.exe" /debug=IMPL
and then it gives me the error:
The application was unable to start correctly (0xc0000007b). Click Ok to close the application.
The odd part of that is, when I do exactly the same actions manually using bat file
"c:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe" "path_to_il.il" /exe /output:"path_to_exe.exe" /debug=IMPL
pause
it does work.
What did I miss?
I think you need to set the file name as well:
processStartInfo.FileName = "ilasm.exe";