2015-10-19 67 views
1

我对C#非常陌生,因为我需要为我的老板制作一个简单的项目。从C#中的OpenFileDialog中选定的文件运行CMD命令

正是我想要得到的是:

  1. 获取选定的文件由用户(打开文件对话框)解密。
  2. 获取用户选定的文件用作密钥。
  3. 运行CMD与功能的OpenSSL ENC -d -AES-256-CBC -in userSelectedFilesToDecrypt .enc退房手续UserSelectedFilesAlreadyDecrypted .MP3文件-pass:./ user_selected_key_file的.bin

就这样。

我已经有了:

  • 打开文件对话框让用户在两个选项(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(); 
} 

我应该如何修改它,才能够使用这个由用户选择的文件吗?感谢您的任何帮助。

回答

0
 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,但这必须帮助你朝着正确的方向发展。

+0

是的,如果您可以测试它会很好,因为我仍然遇到问题。 –

+0

我会在下班后尝试写一个示例应用程序,我不介意等待 –

+0

当然没有。感谢您花时间帮助别人。 –