我开发了一个控制台应用程序,它将带有密码的目录中的文件压缩到目标文件夹。 我正在使用密码将目录中的文件压缩到目标文件夹。
这是代码。在我的本地电脑上工作正常。我已经为这个应用程序创建了设置并安装在我的PC以及其他PC上。在我的电脑上工作正常。但在其他PC上,它给予例外。 你能告诉我可能是什么问题,我该如何解决? 是与此权限相关的问题还是其他一些问题。
{
int codePage = 0;
ZipEntry e = null;
string entryComment = null;
string entryDirectoryPathInArchive = "";
string strFullFilePath = "";
using (ZipFile zip = new ZipFile())
{
//zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently;
// zip.ExtractAll(Environment.CurrentDirectory, .OverwriteSilently);
//var result = zipFile.Any(entry => entry.FileName.EndsWith("input.txt"));
zip.StatusMessageTextWriter = System.Console.Out;
zip.UseUnicodeAsNecessary = true;
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-p":
i++;
if (args.Length <= i) Usage();
zip.Password = (args[i] == "") ? null : args[i];
break;
case "-flat":
entryDirectoryPathInArchive = "";
break;
//case "-utf8":
// zip.UseUnicodeAsNecessary = true;
// break;
case "-64":
zip.UseZip64WhenSaving = Zip64Option.Always;
break;
case "-d":
i++;
if (args.Length <= i) Usage();
string entryName = args[i];
if (!System.IO.Directory.Exists(args[i]))
{
System.IO.Directory.CreateDirectory(args[i]);
//System.Console.Error.WriteLine("Path ({0}) does not exist.", strFullFilePath);
//m_log.Error("Destination Path "+strFullFilePath+" does not exist.");
}
System.DateTime Timestamp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
string strFileName = Timestamp.ToShortDateString();
strFullFilePath = args[i] + strFileName + ".zip";
break;
case "-c":
i++;
if (args.Length <= i) Usage();
entryComment = args[i]; // for the next entry
break;
case "-zc":
i++;
if (args.Length <= i) Usage();
zip.Comment = args[i];
break;
case "-cp":
i++;
if (args.Length <= i) Usage();
System.Int32.TryParse(args[i], out codePage);
if (codePage != 0)
zip.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding(codePage);
break;
case "-s":
i++;
if (args.Length <= i) Usage();
zip.AddDirectory(args[i], args[i]);
//entryDirectoryPathInArchive = args[i];
break;
default:
// UpdateItem will add Files or Dirs, recurses subdirectories
//zip.UpdateItem(args[i], entryDirectoryPathInArchive);
// try to add a comment if we have one
if (entryComment != null)
{
// can only add a comment if the thing just added was a file.
if (zip.EntryFileNames.Contains(args[i]))
{
e = zip[args[i]];
e.Comment = entryComment;
}
else
Console.WriteLine("Warning: ZipWithEncryption.exe: ignoring comment; cannot add a comment to a directory.");
// reset the comment
entryComment = null;
}
break;
}
}
zip.Save(strFullFilePath);
}
}
这些是异常消息
Saving....
Exception: System.IO.DirectoryNotFoundException: Could not find a part of the pa
th.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Move(String sourceFileName, String destFileName)
at Ionic.Zip.ZipFile.Save()
at Ionic.Zip.ZipFile.Save(String zipFileName)
at Ionic.Zip.Examples.ZipWithEncryption.Main(String[] args)
Exception:
Exception: Could not find a part of the path.
Exception: mscorlib
Exception: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullP
ath)
at System.IO.File.Move(String sourceFileName, String destFileName)
at Ionic.Zip.ZipFile.Save()
at Ionic.Zip.ZipFile.Save(String zipFileName)
at Ionic.Zip.Examples.ZipWithEncryption.Main(String[] args)
Exception: Void WinIOError(Int32, System.String)
答案 0 :(得分:1)
我认为这段代码很奇怪:
System.DateTime Timestamp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
string strFileName = Timestamp.ToShortDateString();
strFullFilePath = args[i] + strFileName + ".zip";
首先,你可以写
DateTime Timestamp = DateTime.Now;
然后ToShortDateString
取决于CurrentCulture。强迫你想要的东西(例如:)
String strFileName = Timestamp.ToString("yyyyMMdd");
在我看来,这是例外。如果文化错误,您可以在格式字符串中添加“/”。
然后,使用Path.Combine
而不是操作符+(没有丢失斜杠的错误)
strFullFilePath = Path.Combine(args[i], strFileName + ".zip");
修改强> 对于那些无法格式化DateTime的人来说,这里是我的代码(并且有效):
public static class Constantes
{
public const String DateTimePattern = "yyyyMMddHHmmss";
}
public class SaveProcessViewModel : NotificationObject
{
[...]
String zipFileName = System.Environment.MachineName + "-" + DateTime.Now.ToString(Constantes.DateTimePattern) + ".zip";
String tempZipFile = Path.Combine(Path.GetTempPath(), zipFileName);
zip.Save(tempZipFile);
[...]
}