Skip to content

Instantly share code, notes, and snippets.

@dixsonhuie
Last active July 25, 2023 17:47
Show Gist options
  • Save dixsonhuie/b91c6f662aaf3fe2bdd5539becaacb60 to your computer and use it in GitHub Desktop.
Save dixsonhuie/b91c6f662aaf3fe2bdd5539becaacb60 to your computer and use it in GitHub Desktop.
dotnet examples
// c# start process example
public class StartProcess
{
public void Start(string command, string args, string workingDir)
{
try
{
Process proc = new Process();
// StartInfo
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = args;
proc.StartInfo.WorkingDirectory = workingDir;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
Console.WriteLine("About to start {0} {1} in directory {2}", command, args, workingDir);
Boolean? started = proc.Start();
Console.WriteLine("process id is: {0}", proc.Id);
if (started == null)
{
string errMsg = String.Format("Error while running {0} {1} in directory {2}", command, args, workingDir);
throw new InvalidOperationException(errMsg);
}
// Read the stdout, stderr
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = proc.StandardError.ReadToEnd();
Console.WriteLine("Errors were encountered while running process: {0}", err);
// make process Start a blocking call
int timeoutInMillis = (5 * 60 * 1000);
bool waitForExitStatus = proc.WaitForExit(timeoutInMillis);
Console.WriteLine("process exited with code: {0}", proc.ExitCode);
// TODO - check status and implement kill?
proc.Close();
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
// get datbase tablename from NHibernate from Type of class - entryType below
var classMetaData = SessionFactory.GetClassMetadata(entryType) as NHibernate.Persister.Entity.AbstractEntityPersister;
string tableName = classMetaData.TableName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment