Hi,

I'm new to c#.net world....
i'm currently in doing a program , that has to take the prefix of the 2 textfile (which has the system time as the prefix)., and compare the time of creation of second file with first file.
example : the filename would be like... logger-12-42-41-193 and logger-12-43-45-454

i didnt know how to do that.....please anyone help me finding out the solution...

thanks in advance

Dani AI

Generated

— the simplest, most reliable choice is to stop depending on fragile string parsing when you can trust the filesystem timestamp; if that is not available or you must use the filename, parse the timestamp string with a strict format and compare DateTime values.

As and hinted, use the file metadata when it is authoritative. Example (read the creation time in UTC and compare):

var fi1 = new FileInfo(path1);
var fi2 = new FileInfo(path2);
DateTime t1 = fi1.CreationTimeUtc;
DateTime t2 = fi2.CreationTimeUtc;
bool secondIsNewer = t2 > t1;

If the timestamp is embedded in the name (your example: logger-12-42-41-193), parse it with a strict pattern so parsing errors are handled cleanly. Use DateTime.TryParseExact with an invariant culture and a known format (here HH-mm-ss-fff) and validate the result before comparing:

string name = Path.GetFileNameWithoutExtension(path); // "logger-12-42-41-193"
string ts = name.Substring(name.LastIndexOf("logger-") + "logger-".Length); // "12-42-41-193"
if (DateTime.TryParseExact(ts, "HH-mm-ss-fff", CultureInfo.InvariantCulture,
        DateTimeStyles.None, out DateTime parsed))
{
    // compare parsed to other parsed or to file metadata
}

Notes and caveats: file creation times can change when files are copied; use UTC timestamps to avoid timezone/DST surprises; if the filename only contains a time-of-day (no date) it is ambiguous across midnight — consider storing a full date in the name or fall back to the file timestamp; for large directories prefer Directory.EnumerateFiles to avoid loading all names into memory. Validate with TryParseExact rather than Parse to avoid exceptions.

Recommended Answers

All 3 Replies

See the documentation of (System.IO.FileInfo) class it would help you

What Ramy said plus specifically look at FileInfo to get the file name, use the name property with the string.split function to break it up. If you are scanning an entire directory then consider looking at the DirectoryInfo's getfiles to get an array of fileinfo.

Thanks Jerry for your extra help (Y)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.