1

My requirement is to read a file location retrieve the list of .jpg & .xml files along with their timestamp and write it to a file.

I am new to C#, so far i have been able to get the file list and write the output to a file, but i am not sure how to get the time stamp of file and write it along with list.

I have added code existing code, i would need to have a timestamp for every file in list so that I can use these details for a comparison downstream.

Please advise.

Code

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

Output

C:\test\chq.xml C:\test\img_1.jpg C:\test\img_2.jpg

user2385057
  • 537
  • 3
  • 6
  • 21

3 Answers3

2

Depends which timestamp you're after. You can get the creation time using:

new FileInfo(filename).CreationTime;

That'll give you a DateTime. So you can just do:

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

The .ToString() is optional - you can use it to control the date/time format used in your output. There's also a modified time property you can use if you want that instead.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • string[] array_xml = FileInfo(@"C:\test", "*.xml").CreationTime.ToString(); This code is throwing out two errors : System.IO.FileInfo does not contain a constructor that contains two arguments and System.IO.FileInfo is type but used as variable – user2385057 May 15 '13 at 09:52
  • creating a string string temp = "@C:\test " + "*.xml" ; throws out same error – user2385057 May 15 '13 at 09:56
  • @user2385057 - that line I gave you is supposed to go inside the `foreach` loops. It doesn't produce an array. `new FileInfo` takes the filename as its constructor parameter, and returns a `FileInfo` instance with extra details about the file. – PhonicUK May 15 '13 at 10:00
0

As you said you've just started learning I've put together this slightly modified code to show you where you can simplify some things - I've also removed reading and writing to the 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);
                }
            }
        }
    }
}
RobH
  • 3,604
  • 1
  • 23
  • 46
0

You can try writing code as below:

//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);
    }
}
Adil Mammadov
  • 8,476
  • 4
  • 31
  • 59