C#应用程序GUI和命令行 [英] C# application both GUI and commandline

查看:233
本文介绍了C#应用程序GUI和命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个GUI应用程序。

有没有可能使用从命令行这个相同的应用程序(不包括图形用户界面和使用参数)。

还是我来为命令行工具单独的.exe文件(和应用程序)?


解决方案

  1. 编辑您的项目属性,使您的应用程序一个Windows应用程序(不是控制台应用程序)。您还是可以接受的命令行参数这种方式。如果你不这样做,那么一个控制台窗口会弹出,当你的应用程序的图标上双击。

  2. 确保您的函数接受命令行参数。

  3. 如果你得到任何命令行参数
  4. 请不要显示窗口。

下面是一个简单的例子:

  [STAThread]
静态无效的主要(字串[] args)
{
    如果(args.Length == 0)
    {
        Application.Run(新MyMainForm());
    }
    其他
    {
        //这里执行命令行/静音逻辑...
    }
}

如果您的应用程序尚未结构干净地做沉默的处理(如果您所有的逻辑都塞进你的WinForm code),可以hack沉默的处理ALA CharithJ的回答

由OP修改
对不起,劫持你的答案Merlyn。只是希望所有的信息在这里为别人着想。

要能写在WinForms应用程序安慰只是做到以下几点:

 静态类节目
{
    //定义命令行输出
    函数[DllImport(KERNEL32.DLL)]
    静态外部布尔AttachConsole(INT dwProcessId);
    私人const int的ATTACH_PARENT_PROCESS = -1;    ///<总结>
    ///的主入口点应用程序。
    ///< /总结>
    [STAThread]
    静态无效的主要(字串[] args)
    {
        //控制台重定向输出到父进程;
        //必须是Console.WriteLine任何调用之前()
        AttachConsole(ATTACH_PARENT_PROCESS);        如果(args.Length大于0)
        {
            Console.WriteLine(!耶我刚刚创建了一个命令行工具。);
            //发送回车键是不是真的需要,但除此之外,用户认为应用程序仍通过查看命令行运行。回车键再次发生显示提示的照顾。
            System.Windows.Forms.SendKeys.SendWait({} ENTER);
            Application.Exit();
        }
        其他
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(假);
            Application.Run(新QR codeSampleApp());
        }
    }
}

I currently have an application with a GUI.

Would it be possible to use this same application from the commandline (without GUI and with using parameters).

Or do I have to create a separate .exe (and application) for the commandline tool?

解决方案

  1. Edit your project properties to make your app a "Windows Application" (not "Console Application"). You can still accept command line parameters this way. If you don't do this, then a console window will pop up when you double-click on the app's icon.
  2. Make sure your Main function accepts command line parameters.
  3. Don't show the window if you get any command line parameters.

Here's a short example:

[STAThread]
static void Main(string[] args)
{
    if(args.Length == 0)
    {
        Application.Run(new MyMainForm());
    }
    else
    {
        // Do command line/silent logic here...
    }
}

If your app isn't already structured to cleanly do silent processing (if all your logic is jammed into your WinForm code), you can hack silent processing in ala CharithJ's answer.

EDIT by OP Sorry to hijack your answer Merlyn. Just want all the info here for others.

To be able to write to console in a WinForms app just do the following:

static class Program
{
    // defines for commandline output
    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);
    private const int ATTACH_PARENT_PROCESS = -1;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        // redirect console output to parent process;
        // must be before any calls to Console.WriteLine()
        AttachConsole(ATTACH_PARENT_PROCESS);

        if (args.Length > 0)
        {
            Console.WriteLine("Yay! I have just created a commandline tool.");
            // sending the enter key is not really needed, but otherwise the user thinks the app is still running by looking at the commandline. The enter key takes care of displaying the prompt again.
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Application.Exit();
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new QrCodeSampleApp());
        }
    }
}

这篇关于C#应用程序GUI和命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆