在c#中获取文件时间戳和输出文件列表的管道

本文关键字:文件 输出 列表 管道 时间戳 获取 | 更新日期: 2023-09-27 18:13:44

我的要求是读取文件位置检索.jpg &.xml文件以及它们的时间戳,并将其写入文件。

我是c#的新手,到目前为止,我已经能够获得文件列表并将输出写入文件,但我不确定如何获得文件的时间戳并将其与列表一起编写。

我已经添加了代码现有的代码,我需要有一个时间戳的每个文件在列表中,以便我可以使用这些细节下游比较。

请建议。<标题> 代码

using System;
   using System.Collections;
   using System.Collections.Generic;
   using System.IO;
   using System.Linq;
   using System.Text;
   class Program
   {

    static void Main()
    {
        TextWriter tw = new StreamWriter(@"C:'test'logfile_c#.txt");
        // Put all xml file names in directory into array.
        string[] array1 = Directory.GetFiles(@"C:'test","*.xml");
        // Put all jpg files in directory into array.
        string[] array2 = Directory.GetFiles(@"C:'test", "*.jpg");

        // Display all XML files and write to text file.
        Console.WriteLine("--- XML Files: ---");
        foreach (string name in array1)
        {
            Console.WriteLine(name);
            tw.WriteLine(name);
            Console.ReadLine();
        }
        // Display all JPG files and write to text file..
        Console.WriteLine("--- JPG Files: ---");
        foreach (string name in array2)
        {
            Console.WriteLine(name);
            tw.WriteLine(name);
            Console.ReadLine();
        }
        tw.Close();
    }
    }
<标题> 输出

C: ' ' chq.xml测试C: ' ' img_1.jpg测试C: ' test ' img_2.jpg

在c#中获取文件时间戳和输出文件列表的管道

取决于您所追求的时间戳。您可以使用以下命令获取创建时间:

new FileInfo(filename).CreationTime;

会得到一个DateTime。所以你可以输入:

tw.WriteLine(new FileInfo(name).CreationTime.ToString());

.ToString()是可选的-您可以使用它来控制输出中使用的日期/时间格式。如果你需要的话,你也可以使用修改后的时间属性

正如你所说,你刚刚开始学习,我已经把这个稍微修改的代码放在一起,向你展示你可以简化一些事情-我还删除了读写Console

using System.IO;
namespace Test
{
    class Program 
    {
        static void Main(string[] args)
        {
            // try to name your variables in a meaningful way.
            string[] xmlFiles = Directory.GetFiles(@"C:'test", "*.xml");
            string[] jpgFiles = Directory.GetFiles(@"C:'test", "*.jpg");
            // File.CreateText creates a new file and returns a stream writer.
            // wrap it in a using statement so you don't have to worry about closing it yourself
            using (var writer = File.CreateText(@"C:'test'logfile_c#.txt"))
            {
                FileInfo fi;
                foreach (string name in xmlFiles)
                {
                    // FileInfo instances will give you access to properties
                    // of the file, including the creation date and time.
                    fi = new FileInfo(name);
                    // Use an overload of WriteLine using a format string.
                    writer.WriteLine("file name: {0}, creation time: {1}", name, fi.CreationTime);
                }
                foreach (string name in jpgFiles)
                {
                    fi = new FileInfo(name);
                    writer.WriteLine("file name: {0}, creation time: {1}", name, fi.CreationTime);
                }
            }
        }
    }
}

你可以试着这样写代码:

//Select directory
DirectoryInfo dirInfo = new DirectoryInfo("D:''test");
//Get .xml files
FileInfo[] xmlFiles = dirInfo.GetFiles("*.xml");
//Get .jpg files
FileInfo[] jpgFiles = dirInfo.GetFiles("*.jpg");
//Merge files together for convenience
List<FileInfo> allFiles = new List<FileInfo>();
allFiles.InsertRange(allFiles.Count, xmlFiles);
allFiles.InsertRange(allFiles.Count, jpgFiles);
//Loop through all files and write their name and creation time to text file
using (StreamWriter writer = new StreamWriter("D:''test''test.txt"))
{
    foreach (FileInfo currentFile in allFiles)
    {
        //Format string in desired format
        string info = string.Format("Name: {0}  Creation time: {1}", currentFile.Name, currentFile.CreationTime);
        writer.WriteLine(info);
    }
}