簡體   English   中英

在C#中從OpenFileDialog對選定文件運行CMD命令

[英]Running CMD command on selected files from OpenFileDialog in C#

我是C#的新手,因為我需要為老板做一個簡單的項目。

我想收到的正是:

  1. 獲取要由用戶解密的選定文件(OpenFileDialog)。
  2. 按用戶獲取所選文件以用作鍵。
  3. 使用功能openssl運行CMD enc -d -aes-256-cbc -in userSelectedFilesToDecrypt .enc -out UserSelectedFilesAlreadyDecrypted .mp3 -pass文件:./ user_selected_key_file .bin

就這樣。

我已經得到了:

  • OpenFileDialog供用戶在兩個選項(1.和2)上使用

我需要的:

  • 輸入/輸出的代碼。

這是我當前的代碼:

{
    Process decrypt = new Process();
    string processExecutable = @"C:\Windows\System32\cmd.exe";
    if (!File.Exists(processExecutable))
        throw new ApplicationException(string.Format("The executable file \"{0}\" does not exist.", processExecutable));
    decrypt.StartInfo.FileName = processExecutable;

    decrypt.StartInfo.Arguments = "/C openssl enc -d -aes-256-cbc -in FILES_IN.enc -out FILES_OUT.mp3 -pass file:./KEY_FILE.bin";
    decrypt.StartInfo.UseShellExecute = false;
    decrypt.StartInfo.RedirectStandardOutput = true;
    decrypt.StartInfo.RedirectStandardError = true;
    decrypt.StartInfo.RedirectStandardInput = true;
    decrypt.StartInfo.CreateNoWindow = true;

    decrypt.Start();
    decrypt.BeginOutputReadLine();
    decrypt.BeginErrorReadLine();
    decrypt.WaitForExit();
    decrypt.Close();
}

我應該如何修改它,以便能夠在用戶選擇的文件上使用它? 感謝您的任何幫助。

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            DialogResult dr = ofd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String file in ofd.FileNames)
                {
                    listView1.Items.Add(file);

                    getCommandlineResult(file, "path1", "path2");
                }
            }

        }

        public string getCommandlineResult(string userSelectedFilesToDecrypt, string UserSelectedFilesAlreadyDecrypted,
            string user_selected_key_file)
        {
            string macAddress = string.Empty;
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
            pProcess.StartInfo.Arguments = "openssl enc - d - aes - 256 - cbc -in " + userSelectedFilesToDecrypt + ".enc -out " +
                                           UserSelectedFilesAlreadyDecrypted + ".mp3 - pass file:./ " +
                                           user_selected_key_file + ".bin";
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow = true; //hide window
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();
            return strOutput;
        }

您必須自己調整代碼,因為我沒有安裝openssl,但這必須在正確的方向上為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM