简体   繁体   English

如何使用密码正确格式化 ac# 7-zip 字符串?

[英]How to correctly format a c# 7-zip string with a password?

So I am coding an encryption application in C#, which calls on 7zip to encrypt a folder into a 7zip archive with a previous entered password.所以我正在用 C# 编写一个加密应用程序,它调用 7zip 使用先前输入的密码将文件夹加密为 7zip 存档。 The trouble is, for some reason it's seeing the file I am trying to compress into the 7zip archive as a 7zip file itself, when it actually is just a normal folder so not sure why that's happening.问题是,由于某种原因,它看到我试图压缩到 7zip 存档中的文件作为 7zip 文件本身,而实际上它只是一个普通文件夹,所以不确定为什么会发生这种情况。 Here is the code:这是代码:

string sourceName = @"c:\putfilesforencryptionhere";
string targetName = @"c:\encryptedfileshere.7z";
string password = Form2.verify;

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

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

And when running the program, 7zip returns this error:并且在运行程序时,7zip 返回此错误:

The filename, directory name, or volume label syntax is incorrect.文件名、目录名或卷标语法不正确。 cannot open file c:\\encryptedfileshere.7z -p09/28/2020 11:17:29 AM c:\\putfilesforencryptionhere.7z.无法打开文件 c:\\encryptedfileshere.7z -p09/28/2020 11:17:29 AM c:\\putfilesforencryptionhere.7z。

String password = Form2.verify as it is a password that is entered in a previous form. String password = Form2.verify因为它是在以前的表单中输入的密码。 I wonder why 7-zip would see c:\\putfilesforencryptionhere as a 7zip file when it is a normal folder?我想知道为什么 7-zip 将 c:\\putfilesforencryptionhere 视为普通文件夹时的 7zip 文件?

Thanks very much.非常感谢。

When setting the value of p.Arguments , there is an escaped quote \\" before targetName but not after. Therefore, the entirety of the following string is being interpreted as the archive name (as seen in the error message).设置p.Arguments的值时,在targetName之前而不是之后有一个转义的引号\\" 。因此,整个以下字符串被解释为存档名称(如错误消息中所示)。

Try尝试

p.Arguments = "a \"" + targetName + "\" -p" + password + " " + sourceName;

Or use ArgumentList to avoid escaping issues.或者使用ArgumentList来避免转义问题。

p.ArgumentList.Add("a");
p.ArgumentList.Add(targetName);
p.ArgumentList.Add("-p" + password);
p.ArgumentList.Add(sourceName);

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

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