繁体   English   中英

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

[英]Passing values from windows form to console application

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

您可能需要添加while循环以在控制台应用程序中查找txt文件。 在Windows窗体应用程序中,您可以将成功或失败消息写入txt文件。 (为安全起见,添加加密)在您记下信息时,控制台应用程序应阅读该信息并从那里继续。

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

由于该表单也位于控制台应用程序项目中(我以您的措辞为准),因此您可以执行以下操作

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();
    }
}

和登录表单类中的登录按钮单击事件

 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;
        }
    }

谢谢,

也先

您可以在表单应用程序中托管wcf服务。 然后使控制台应用程序成为客户端。 使用wcf创建面向服务的系统非常容易。 请参阅此处的教程。 如果您采用这种方法,将会学到很多东西

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

本质上,您可能必须使用某些本机Windows API函数(Alloc / FreeConsole)并使用WinForms控制器。

半伪代码:

[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!
}

从本质上讲,这段代码所做的是执行“ GUI”验证步骤,然后,如果成功,它将尝试为应用程序分配一个控制台。 分配控制台后,它将通过完全隐藏GUI并仅显示新控制台进入“控制台模式”(关闭控制台会关闭应用程序)。 执行您的“控制台模式”代码,然后关闭控制台,然后GUI返回。

暂无
暂无

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

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