Start Process


Node Information
Starts a process, choose a file path to start, or a web url to start with the default browser.
General Information
• This node doesn't have other available options.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Start IN RUN MULTIPLE FALSE FALSE
File IN String ONE FALSE FALSE
Arguments IN String ONE FALSE FALSE
Verb IN String ONE FALSE FALSE
UserName IN String ONE FALSE FALSE
Password IN String ONE FALSE FALSE
WindowStyle IN ProcessWindowStyle ONE FALSE FALSE
UseShellExecute IN Boolean ONE FALSE FALSE
CreateNoWindow IN Boolean ONE FALSE FALSE
WaitForExit IN Boolean ONE FALSE FALSE
WaitTime IN Int32 ONE FALSE FALSE
Exited OUT RUN MULTIPLE FALSE FALSE
StandardOutput OUT String MULTIPLE FALSE FALSE
RedirectStandardOutput IN Boolean ONE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Win32.Processes
{
    [IsNode("Start Process")]
    public class StartProcess : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Start");
        Port file = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "File");
        Port args = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Arguments");
        Port verb = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Verb");
        Port usr = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "UserName");
        Port pwd = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Password");
        Port createnowin = new Port(PortType.IN, typeof(bool), AcceptsLinks.ONE, "CreateNoWindow");
        Port useshell = new Port(PortType.IN, typeof(bool), AcceptsLinks.ONE, "UseShellExecute");
        Port winstyle = new Port(PortType.IN, typeof(System.Diagnostics.ProcessWindowStyle), AcceptsLinks.ONE, "WindowStyle");
        Port wait = new Port(PortType.IN, typeof(bool), AcceptsLinks.ONE, "WaitForExit");
        Port waittime = new Port(PortType.IN, typeof(int), AcceptsLinks.ONE, "WaitTime");
        Port exited = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Exited");
        Port standardoutput = new Port(PortType.OUT, typeof(string), AcceptsLinks.MULTIPLE, "StandardOutput");
        Port redirstandardoutput = new Port(PortType.IN, typeof(bool), AcceptsLinks.ONE, "RedirectStandardOutput");
        private bool Wait
        {
            get { bool ret = false; bool.TryParse(GetRuntimeValue(wait).ToString(), out ret); return ret; }
        }
        private int WaitTime
        {
            get { int ret = 0; int.TryParse(GetRuntimeValue(waittime).ToString(), out ret); return ret; }
        }
        private bool Redirect { get { return GetRuntimeValue(redirstandardoutput); } }
        public StartProcess()
        {
            Name = "Start Process";
            NodeType = KGraph.NodeType.Method;
            AddPort(run);
            AddPort(file);
            AddPort(args);
            AddPort(verb);
            AddPort(usr);
            AddPort(pwd);
            AddPort(winstyle);
            AddPort(useshell);
            AddPort(createnowin);
            AddPort(wait);
            AddPort(waittime);
            AddPort(exited);
            AddPort(standardoutput);
            AddPort(redirstandardoutput);
            standardoutput.IsVariable = true;
            standardoutput.VariableType = typeof(string);
            Information = "Starts a process, choose a file path to start, or a web url to start with the default browser.";
        }
        public override string Code()
        {
            string ar = GetValue(args);
            string u = GetValue(usr);
            string p = GetValue(pwd);
            string v = GetValue(verb);
            string arg = string.Empty;
            string us = string.Empty;
            string pw = string.Empty;
            string ve = string.Empty;
            string stout = string.Empty;
            standardoutput.Instructions = standardoutput.ID;
            if (ar != "@\"\"")
            {
                arg = "psi.Arguments = " + ar + ";";
            }
            if (u != "@\"\"")
            {
                us = "psi.UserName = " + u + ";";
            }
            if (p != "@\"\"")
            {
                pw = "System.Security.SecureString secstring = new System.Security.SecureString();\r\n" +
                     "foreach(char ch in " + p + "){ secstring.AppendChar(ch); }\r\n" +
                     "psi.Password = secstring;\r\n";
            }
            if (v != "@\"\"")
            {
                ve = "psi.Verb = " + v + ";";
            }
            if (Redirect)
            {
                stout = standardoutput.ID + " = process.StandardOutput.ReadToEnd();";
            }
            string code =
            "System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();\r\n" +
            arg + Environment.NewLine + ve + Environment.NewLine + us + Environment.NewLine + pw + Environment.NewLine +
            "psi.CreateNoWindow = " + GetValue(createnowin) + ";\r\n" +
            "psi.UseShellExecute = " + GetValue(useshell) + ";\r\n" +
            "psi.WindowStyle = " + GetValue(winstyle) + ";\r\n" +
            "psi.FileName = " + GetValue(file) + ";\r\n" +
            "psi.RedirectStandardOutput = " + GetValue(redirstandardoutput) + ";\r\n" +
            "System.Diagnostics.Process process = new System.Diagnostics.Process();\r\n" +
            "process.StartInfo = psi;\r\n" +
            "process.EnableRaisingEvents = true;\r\n" +
            "process.Exited += delegate(object sender, EventArgs e)\r\n{\r\n" + GetValue(exited) + "};\r\n" +
            "process.Start();\r\n";
            if (Wait)
            {
                if (WaitTime > 0)
                {
                    code += Environment.NewLine + "process.WaitForExit(" + GetValue(waittime) + ");\r\n";
                }
                else
                {
                    code += Environment.NewLine + "process.WaitForExit();\r\n";
                }
            }
            code += stout;
            return code;
        }
    }
}