我如何在c#

时间:2018-07-09 20:01:43

标签: c#

我的代码:

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文件(逐行)?

1 个答案:

答案 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());
            }
        }

输出:

  1. file1.exe
  2. file5.exe
  3. 文件夹1
  4. Folder2 \ file3.pdf
  5. Folder2 \ file4.msi

感谢所有答复。