【问题标题】:Running CMD command on selected files from OpenFileDialog in C#从 C# 中的 OpenFileDialog 对选定文件运行 CMD 命令
【发布时间】:2016-01-17 16:59:44
【问题描述】:

我对 C# 很陌生,因为我需要为我的老板做一个简单的项目。

我想收到的正是:

  1. 获取选定的文件以供用户解密 (OpenFileDialog)。
  2. 获取用户选择的文件以用作键。
  3. 使用函数 openssl 运行 CMD enc -d -aes-256-cbc -in userSelectedFilesToDecrypt.enc -out UserSelectedFilesAlreadyDecrypted.mp3 -pass file:./ 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();
}

我应该如何修改它,以便能够在用户选择的文件上使用它?感谢您的帮助。

【问题讨论】:

    标签: c# cmd openssl openfiledialog


    【解决方案1】:
            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,但这必须帮助您朝着正确的方向前进。

    【讨论】:

    • 是的,如果你能测试它会很棒,因为我仍然有这个问题。
    • 今晚下班后我会尝试写一个示例应用,你不介意等待
    • 不,当然。感谢您将空闲时间花在帮助他人上。
    • @ŁukaszLipiński 我昨晚添加了它,我想知道您是否取得了任何进展或有后续问题
    • 我为 OpenSSL 创建了新课程。我以不同的方式制作了这个,但希望得到建议;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2022-01-05
    • 2019-07-01
    • 2018-03-14
    相关资源
    最近更新 更多