首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 實作 QQ 自動登入器(一):主視窗、命令列啟動與持久化整合

     編輯:關於C語言

專案緣起

發現 QQ 支援命令列登入:`QQ路徑 /start QQUIN:帳號 PWDHASH:MD5+Base64密碼 /stat:模式`。嘗試有效,遂開發圖形化登入器,採序列化持久化用戶資訊。

主視窗 `QQLoginForm` 核心流程

登入按鈕邏輯


private void button1_Click(object sender, EventArgs e)
{
    if (_ui == null) _ui = new UserInfo();
    _ui.Username = txtUser.Text.Trim();
    _ui.PassWord = txtPwd.Text;
    _ui.Type = cboType.Text == "正常" ? "41" : "40";

    if (!ValidateInput()) return;

    if (string.IsNullOrEmpty(_ui.Path))
    {
        if (opfQQ.ShowDialog() == DialogResult.OK)
            _ui.Path = opfQQ.FileName;
        else return;
    }
    LoginQQ(_ui.Username, _ui.PassWord, _ui.Type, _ui.Path);
    SerializeHelper.SerializeUserInfo(_ui); // 每次登入自動儲存
}

命令列啟動 QQ


private void LoginQQ(string user, string pwd, string type, string path)
{
    var proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        }
    };
    proc.Start();
    proc.StandardInput.WriteLine($"{path} /start QQUIN:{user} PWDHASH:{EncodeHash.PwdHash(pwd)} /stat:{type}");
    proc.StandardInput.WriteLine("exit");
}
  • 隱藏命令視窗(`CreateNoWindow=true`)。
  • 重導向標準輸入,寫入 QQ 啟動命令後送 `exit` 結束 `cmd.exe`。
  • 密碼即時經 `EncodeHash.PwdHash` 轉換(MD5+Base64)。

輸入限制:QQ 號僅數字


private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
        e.Handled = true; // 忽略非數字/退格鍵
}

表單載入自動填入


private void QQLoginForm_Load(object sender, EventArgs e) => LoadInfo();
private void LoadInfo()
{
    _ui = SerializeHelper.DeserializeUserInfo();
    if (_ui != null)
    {
        txtUser.Text = _ui.Username;
        txtPwd.Text = _ui.PassWord;
        cboType.SelectedIndex = _ui.Type == "41" ? 0 : 1;
    }
    else cboType.SelectedIndex = 0;
}

設定按鈕開啟 `ConfigForm`


private void btnConfig_Click(object sender, EventArgs e)
{
    new ConfigForm().ShowDialog();
    LoadInfo(); // 同步可能被修改的路徑/模式
}

架構總結

  • 三層分離:UI(`QQLoginForm`/`ConfigForm`)→ 業務邏輯(`LoginQQ`/`ValidateInput`)→ 資料存取(`SerializeHelper`/`EncodeHash`)。
  • 單一職責:每個類別專注單一領域,易維護、易測試。
  • 序列化透明化:UI 無感知儲存格式,遷移至 JSON/SQLite 僅改 `SerializeHelper`。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved