开发者

Passing arguments to a console application from C#

i need to run console applications from another c# application,ho开发者_如何学Gow to load & pass arguments to console application from my c# application in order to execute the console application. is System.Diagnostics.ProcessStartInfo will help?


Use ProcessStartInfo class

       ProcessStartInfo p = new ProcessStartInfo();
       p.Arguments = "your arguments";
       p.FileName = "Application or Document Name";
       Process.Start(p);





    public IList<string> GetMatchingWords(string word)
    {
        var list = new List<string>();

        int levelDepth = 0;
        if (string.IsNullOrEmpty(word))
            return list;

        var tempWord = word[0];
        var firstNode = RootNode.Childs.FirstOrDefault(x => x.Word[0].Equals(tempWord));

        if (firstNode == null)
        {
            return list;
        }


        var nodePath = new Queue<TrieNode>();

        var sb = new StringBuilder();
        sb.Append(firstNode.Word);



        while (firstNode != null)
        {


            levelDepth++;

            if (levelDepth == word.Length)
            {
                break;
            }



            tempWord = word[levelDepth];
              firstNode = firstNode.Childs.FirstOrDefault(x => x.Word[0].Equals(tempWord));

              sb.Append(firstNode.Word);

        }

        if(firstNode!=null)
            nodePath.Enqueue(firstNode);


        originalValue = sb.ToString();


        while (nodePath.Any())
        {
            var tempNode = nodePath.Dequeue();

            tempNode.IsParentNode = true;

            PopulateWords(tempNode, sb, ref list);

        }


        return list;
    }

    private void PopulateWords(TrieNode node,
         StringBuilder sb,ref List<string> list)
    {



        if (node.Childs.Any())
        {
            foreach (var temp in node.Childs)
            {
                if (node.IsParentNode)
                {
                    sb.Clear();
                    sb.Append(originalValue);
                }

                if (temp.Childs.Any())
                {

                    sb.Append(temp.Word);
                    PopulateWords(temp, sb, ref list);

                }
                else
                {
                    sb.Append(temp.Word);
                    list.Add(sb.ToString());
                }

            }
        }
        else
        {

            list.Add(sb.ToString());

        }


    }


With Process.Start, the second parameter is the parameter to the application:

Process.Start("IExplore.exe", "www.northwindtraders.com");

The above example is from http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx. This contains more examples on how to start a process.


We can pass arguments to a console application in two ways. here with i have written a program showing both the methodologies. i think it will be very usefull

using System;
using System.Diagnostics;
using System.ComponentModel;
namespace ProcessSample
{
    class ClsProcess
    {
        void OpenProcessWithArguments()
        {
            Process.Start("IExplore.exe", "www.google.com");
        }

        void OpenProcessWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);

            startInfo.Arguments = "www.google.com";

            Process.Start(startInfo);
        }

        static void Main()
        {
            ClsProcess myProcess = new ClsProcess();

            myProcess.OpenProcessWithArguments();
            myProcess.OpenProcessWithStartInfo();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜