--- /dev/null
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessLauncher", "ProcessLauncher\ProcessLauncher.csproj", "{8C9CE3D4-590C-49DA-A165-9B82625E4A3E}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {8C9CE3D4-590C-49DA-A165-9B82625E4A3E}.Debug|x86.ActiveCfg = Debug|x86
+ {8C9CE3D4-590C-49DA-A165-9B82625E4A3E}.Debug|x86.Build.0 = Debug|x86
+ {8C9CE3D4-590C-49DA-A165-9B82625E4A3E}.Release|x86.ActiveCfg = Release|x86
+ {8C9CE3D4-590C-49DA-A165-9B82625E4A3E}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
+ GlobalSection(MonoDevelopProperties) = preSolution
+ StartupItem = ProcessLauncher\ProcessLauncher.csproj
+ EndGlobalSection
+EndGlobal
--- /dev/null
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Text;
+
+namespace ProcessLauncher
+{
+ public class LinuxWindowsProcess
+ {
+ public StringBuilder Test(string command, string arguments)
+ {
+ ProcessStartInfo startInfo = new ProcessStartInfo();
+
+ // What is going to do Mono with this value?
+ startInfo.CreateNoWindow = true;
+
+ // Set UseShellExecute to false for redirection.
+ // To use StandardOutput, you must set ProcessStartInfo.UseShellExecute to false, and you must set
+ // ProcessStartInfo.RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception.
+ // What is going to do Mono with stdout, stdin and stderr depending on this value?
+ startInfo.UseShellExecute = false;
+
+ // Redirect the standard output of the subprocess command.
+ // What is going to do Mono with stdout? Some pipe, I guess.
+ startInfo.RedirectStandardOutput = true;
+
+ // What is going to do Mono with stdin?
+ startInfo.RedirectStandardInput = false;
+
+ // What is going to do Mono with stderr?
+ startInfo.RedirectStandardError = false;
+
+ // What is going to do Mono with this value? What if I am running a GTK (GUI application)
+ startInfo.WindowStyle = ProcessWindowStyle.Minimized;
+
+ startInfo.Arguments = arguments;
+ startInfo.FileName = command;
+
+
+
+
+ // Define variables shared by class methods.
+ StringBuilder processOutput = null;
+ Process process = new Process();
+
+ process.StartInfo = startInfo;
+
+ // When is Mono going to call my event implementation? Every time the subprocess writes to stdout?
+ // From here: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.outputdatareceived%28v=vs.110%29.aspx
+ // Thereafter, the OutputDataReceived event signals each time the process writes a line to the
+ // redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
+ // That behaviour is weird (IMHO) Check out the Mono implementation!!!
+ // Set our event handler to asynchronously read the standard output.
+ process.OutputDataReceived += new DataReceivedEventHandler(
+ delegate(object sendingProcess, DataReceivedEventArgs outLine)
+ {
+ // Collect the command output.
+ // To asynchronously collect the redirected StandardOutput or StandardError stream output of a process, you must create
+ // a method that handles the redirected stream output events. The event-handler method is called when the process writes
+ // to the redirected stream. The event delegate calls your event handler with an instance of DataReceivedEventArgs.
+ // The Data property contains the text line that the process wrote to the redirected stream.
+ if (!String.IsNullOrEmpty(outLine.Data))
+ {
+ // Add the text to the collected output.
+ processOutput.Append(Environment.NewLine + outLine.Data);
+ }
+ });
+ // Taken from http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net
+ // I do not think this is the right way to read the output stream!!!!
+ // My event handler will be called (see documentation) every time the process writes a line to the output stream but
+ // with this code I will be reading the output stream until it is closed by the process. So, I guess, we could end
+ // up having multiple event handlers reading from the same stream (output stream) Each handler was called each time
+ // the process wrote a line to the output stream. :/
+ // process.OutputDataReceived += new DataReceivedEventHandler(
+ // delegate(object sender, DataReceivedEventArgs e)
+ // {
+ // using (StreamReader output = process.StandardOutput)
+ // {
+ // string data = output.ReadToEnd();
+ // processOutput.Append(data);
+ // }
+ // });
+ // Don't mix using statements and lambda expressions :(
+ // http://blogs.msdn.com/b/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx
+ // process.OutputDataReceived += (sender, e) =>
+ // {
+ // using (StreamReader output = process.StandardOutput)
+ // {
+ // string data = output.ReadToEnd();
+ // processOutput.Append(data);
+ // }
+ // };
+
+ // Start the process.
+ process.Start();
+
+ // Start the asynchronous read of the output stream.
+ process.BeginOutputReadLine();
+
+ // Wait for the process.
+ process.WaitForExit();
+
+ process.Close();
+
+ return processOutput;
+ }
+ }
+}
+
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>12.0.0</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{8C9CE3D4-590C-49DA-A165-9B82625E4A3E}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>ProcessLauncher</RootNamespace>
+ <AssemblyName>ProcessLauncher</AssemblyName>
+ <Description>Launching processes from C#</Description>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Externalconsole>true</Externalconsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <DebugType>full</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Externalconsole>true</Externalconsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="LinuxWindowsProcess.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Folder Include="shellscripts\" />
+ </ItemGroup>
+</Project>
\ No newline at end of file
--- /dev/null
+using System;
+
+namespace ProcessLauncher
+{
+ class MainClass
+ {
+ public static void Main(string[] args)
+ {
+ LinuxWindowsProcess process = new LinuxWindowsProcess();
+ Console.WriteLine("Linux");
+ var output = process.Test("ls", "-lah");
+ Console.WriteLine("stdout: {0}", output.ToString());
+
+ // Console.WriteLine("Windows");
+ // output = process.Test("dir", "/a");
+ // Console.WriteLine("stdout: {0}", output.ToString());
+ }
+ }
+}
--- /dev/null
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes.
+// Change them to the values specific to your project.
+[assembly: AssemblyTitle("ProcessLauncher")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("gumartinm.name")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("gumartinm.name")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+[assembly: AssemblyVersion("1.0.*")]
+// The following attributes are used to specify the signing key for the assembly,
+// if desired. See the Mono documentation for more information about signing.
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+