繁体   English   中英

尝试在 C# Visual Studio 中创建 7zip 文件时出错 [重复]

[英]Error when trying to create a 7zip file in C# Visual Studio [duplicate]

所以我试图在 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

任何帮助表示赞赏,谢谢。

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

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

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

 "\\\\" Form2.verify;

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

顺便说一句:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM