简体   繁体   English

将值从Windows窗体传递到控制台应用程序

[英]Passing values from windows form to console application

因此,我有一个程序可以通过Windows窗体获取用户凭据,现在,通过MessageBox,我当前正在显示用户输入,我想要做的就是将其传递到控制台应用程序中,以便如果用户输入正确的凭据然后继续在控制台应用程序中,我该怎么做呢?

you may need to add a while loop to look for a txt file in your console application. 您可能需要添加while循环以在控制台应用程序中查找txt文件。 In your windows forms application, you can write a success or failure message into a txt file. 在Windows窗体应用程序中,您可以将成功或失败消息写入txt文件。 (Add encryption for security) The moment you write down the information your console app should read it and continue from there. (为安全起见,添加加密)在您记下信息时,控制台应用程序应阅读该信息并从那里继续。

Algorithm:
1.console application start
2. console app while loop until txt file detected
3. forms app show input screen
4. user enter credential
5. write success or failure into txt file
6. read txt file
7. continue based on credential result
8. Remove txt file

Since the form is also in the console app project (I assume it from your wordings) you can do the following 由于该表单也位于控制台应用程序项目中(我以您的措辞为准),因此您可以执行以下操作

class Program
{
    public static object abc;
    static void Main(string[] args)
    {
        //do something here if required
        Form1 frm = new Form1();
        if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            //login success do what ever on success
            Console.WriteLine("Login success");
            Console.WriteLine(abc.ToString());
        }
        else
        {
            Console.WriteLine("Login failure");
            Console.WriteLine(abc.ToString());
            //login failure
        }
        Console.ReadLine();
    }
}

and the login button click event in your login form class 和登录表单类中的登录按钮单击事件

 private void Login_Click(object sender, EventArgs e)
    {
        if(true)
        {
            Program.abc = "any success object here";
            //on successful login
        this.DialogResult= System.Windows.Forms.DialogResult.OK;
        }
        else
        {
            Program.abc = "any failure object here";
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }

Thanks, 谢谢,

Esen 也先

you could host a wcf service within your form app. 您可以在表单应用程序中托管wcf服务。 then make the console app a client. 然后使控制台应用程序成为客户端。 creating a service oriented system is surprisingly easy with wcf. 使用wcf创建面向服务的系统非常容易。 See here for a tutorial. 请参阅此处的教程。 you will learn a lot if you follow this approach 如果您采用这种方法,将会学到很多东西

结合使用IPC(进程间通信)和命名管道。在两个进程之间轻松实现,请访问http://msdn.microsoft.com/zh-cn/library/bb546085.aspx

Essentially you'll probably have to use some native Windows API functions (Alloc/FreeConsole) and use a WinForms controller. 本质上,您可能必须使用某些本机Windows API函数(Alloc / FreeConsole)并使用WinForms控制器。

Semi-pseudocode: 半伪代码:

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AllocConsole();

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeConsole();

//--- form code
if (Do_validation() && AllocConsole())
{
    this.Hide();
    this.ShowInTaskbar = false;
    Enter_Console_Code();
    FreeConsole();
    System.Threading.Thread.Sleep(50); //FreeConsole sometimes doesn't finish closing straight away which means your form flickers to the front and then minimizes.
    this.ShowInTaskbar = true;        
    this.Show(); 
}
//---

private void Enter_Console_Code()
{ 
    string line = string.Empty;
    while ((line = Console.ReadLine()) != "q")
        Console.WriteLine(line); //pointless code ftw!
}

Essentially what this code does is do your "GUI" validation step, and then if that's successful it attempts to allocate a console for the application. 从本质上讲,这段代码所做的是执行“ GUI”验证步骤,然后,如果成功,它将尝试为应用程序分配一个控制台。 Once the console is allocated it enters "Console Mode" by completely hiding the GUI and displaying only the new console (closing the console closes the application by the way). 分配控制台后,它将通过完全隐藏GUI并仅显示新控制台进入“控制台模式”(关闭控制台会关闭应用程序)。 Your "Console Mode" code is executed, then the console is closed and the GUI comes back. 执行您的“控制台模式”代码,然后关闭控制台,然后GUI返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM