簡體   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