问题描述
所以我试图在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活动
感谢您的帮助,谢谢。
解决方法
并非所有代码路径都返回值
您的代码中根本没有return
语句。
您不知道该返回什么?然后,使该方法返回void
而不是string
。无论如何,这是按钮事件处理程序的正确签名。
"\\" Form2.verify;
需要一个+
来连接字符串。
还有顺便说一句:
-
x.WaitForExit();
将只等待7Zip结束。它不保证某些内容被压缩。 - 在按钮的事件处理程序中包含所有代码,可能会使您的程序无响应,具体取决于Zip操作花费的时间。 Windows可能会显示“不响应”标题,并且用户可能会杀死您的应用程序。
- 请考虑使通往7Zip的路径可配置,而不是强迫人们将其安装在特定位置。您甚至可以从
HKEY_CLASSES_ROOT\7-Zip.7z\shell\open\command
的注册表中获取路径。