【问题标题】:Error when trying to create a 7zip file in C# Visual Studio [duplicate]尝试在 C# Visual Studio 中创建 7zip 文件时出错 [重复]
【发布时间】:2020-09-27 19:16:11
【问题描述】:

所以我试图在 C# VS Studio 2019 中编写一个脚本,其中用户输入要使用 7zip 加密的文件并选择他们的密码等,使用字符串来决定密码和要加密的文件。它将作为“encryptedfilehere.7z”保存到 c:\ 驱动器。问题是我正在努力正确格式化代码:

 private string button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Assuming 7-zip is installed in C:\\Program Files\\7-Zip\\.  If error message appears 7-zip is not installed in this driectory, it needs to be so.");
        string sourceName = textBox1.Text;
        string targetName = "c:\\encryptedmessagehere.7z";

        // 1
        // Initialize process information.
        //
        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = "C:\\Program Files\\7-Zip\\7z.exe";

        // 2
        // Use 7-zip
        // specify a=archive and -tgzip=gzip
        // and then target file in quotes followed by source file in quotes
        //
        p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\\" Form2.verify;
        p.WindowStyle = ProcessWindowStyle.Hidden;

        // 3.
        // Start process and wait for it to exit
        //
        Process x = Process.Start(p);
        x.WaitForExit();
    }
    

密码是 Form2.verify,因为它需要用户通过名为 verify 的字符串以另一种形式输入的密码。返回的错误是:

错误 CS0161 'Form7.button2_Click(object, EventArgs)': 并非所有代码路径都返回值

使用 Form2.verify 作为密码:

错误 CS0201 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句 ByteOribtPrivacyCannon C:\Users\keife\Downloads\ByteOrbitPrivacyCannon (2)\UniveralWindowsTextPGP\UniveralWindowsTextPGP\Form7.cs 48 Active

感谢您的帮助,谢谢。

【问题讨论】:

  • 错误信息很简单。原因已经在 Stack Overflow 上被讨论到死。有关示例,请参见副本。对于您的第二个错误(这是一个完全不同的问题...不要在一个帖子中问两个问题),请执行您应该为第一个错误做的事情,即搜索该站点(或仅使用您最喜欢的网络搜索)有关错误消息的确切文本。您将获得丰富的信息。

标签: c# .net visual-studio aes 7zip


【解决方案1】:

并非所有代码路径都返回值

您的代码中根本没有 return 语句。

你不知道返回什么?然后让方法返回void 而不是string。无论如何,这是按钮事件处理程序的正确签名。

"\\" Form2.verify;

这需要一个+ 来连接字符串。

顺便说一句:

  • x.WaitForExit(); 将等待 7Zip 结束。它不保证某些内容已被压缩。
  • 在按钮的事件处理程序中包含所有这些代码可能会使您的程序无响应,具体取决于 Zip 操作需要多长时间。 Windows 可能会显示“未响应”标题,并且用户可能会终止您的应用程序。
  • 考虑使 7Zip 的路径可配置,而不是强迫人们将其安装在特定位置。您甚至可以从 HKEY_CLASSES_ROOT\7-Zip.7z\shell\open\command 的注册表中获取路径

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多