我的代码:
public static string Source = @"\\192.168.181.1\Z$\z";
public static string Destination = @"\\192.168.181.1\Z$\z_rar\" + DateTime.Now.ToString("dddd");
public static string Extension = @".zip";
private static void Main(string[] args)
{
if (File.Exists(Destination + Extension))
try
{
File.Delete(Destination + Extension);
ZipFile.CreateFromDirectory(Source, Destination + Extension, CompressionLevel.NoCompression, false);
// here: zipped file details in txt file
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
else
ZipFile.CreateFromDirectory(Source, Destination + Extension, CompressionLevel.NoCompression, false);
}
我如何创建压缩文件的详细信息,并保存到zipfile.createfromdirectory之后的同一目录中的zipfilename_log.txt文件(逐行)?
答案 0 :(得分:0)
我用以下代码解决了这个问题:
public static string Source = @"\\192.168.1.11\Z$\z";
public static string Destination = @"\\192.168.1.11\Z$\z_rar\";
public static string NameFormat = DateTime.Now.ToString("dddd");
public static string Extension = @".zip";
public static string LogFilePath = Destination + NameFormat + "_log.txt";
private static void Main(string[] args)
{
File.Delete(Destination + NameFormat + Extension);
File.Delete(LogFilePath);
try
{
ZipFile.CreateFromDirectory(Source, Destination + NameFormat + Extension,
CompressionLevel.NoCompression, false);
var fileListBuilder = new StringBuilder();
using (var za = ZipFile.OpenRead(Destination + NameFormat + Extension))
{
foreach (var zae in za.Entries) fileListBuilder.AppendLine(zae.FullName);
}
File.WriteAllText(LogFilePath, fileListBuilder.ToString());
}
catch (IOException e)
{
File.WriteAllText(LogFilePath, e.ToString());
}
}
输出:
感谢所有答复。