Use DateTime.ToFileTime and FromFileTime through file name string - c#

I want to save to a specific space a single file at several states. So i thought about stamping it with the time of creation to tell one from another.
I have created a dummy program that goes through exactly the same procedure my full program does but concentrated.
Here is the code:
DateTime first = DateTime.Now;
long ft = first.ToFileTime();
string time = "" + ft;
long timeLong = Convert.ToInt64(time);
DateTime normalDate = DateTime.FromFileTime(timeLong);
string normalName = ""+ normalDate;
DateTime thisDate = DateTime.Parse(normalName);
long fit = thisDate.ToFileTime();
So it goes through those steps:
Create time and convert to long for file.
Save it as string inside the files name(i pass it like that but in "" is the files name)
Then i retrieve it, i cast it into long, to create a DateTime format readable for the human with FromFileTime.Then to string fro the user to read it in a comboBox.
Then the user selects and it becomes DateTime , to get into long through tge TiFileTime ( at this point i have the problem as it appears to loose everything smaller than seconds)
I want to cast it back into long at the end to be able to run through the files again and find the one that matches with the one that the user choose.
here is some output values:
Edit : As you can see form the results above. I miss something . The initial and the final values are not the same.So i cant find the same file again.

The Parse() method don't have all the information that the DateTime structure holds (you can find its members here).
string normalName = ""+ normalDate;
DateTime thisDate = DateTime.Parse(normalName);
Will not return the exact value (including Ticks) as the original one.
You should do it as follows, using Ticks:
string normalName = "" + normalDate.Ticks;
DateTime thisDate = new DateTime(long.Parse(normalName));
long fit = thisDate.ToFileTime();

Related

Get DateTime from FileName

I have a file named test-2000_01_02-10_12_14.xml.
How do I only get the date from the file?
I was able to get the date if the file has this name: 2000_01_02-10_12_14
with this (b is a StorageFile):
DateTime dateVal;
bool parsed = DateTime.TryParseExact(b.DisplayName,
"yyyy_MM_dd-H_mm_ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out dateVal);
I then tried to change yyyy_MM_dd-H_mm_ss to something like this *-yyyy_MM-dd-H-mm_ss but it does not seem to be the solution
There are a boatload of ways to do this, it really rather depends on how regular the naming of your files is - is there always some junk text followed by a hyped, then the year?
Post up another 10 different examples if you want more tailored advice. Here's a way for the one you've posted:
DateTime.TryParseExact(
Path.GetFileNameWithoutExtension(b.DisplayName.Substring(b.DisplayName.IndexOf('-')+1)),
"yyyy_MM_dd-H_mm_ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out dateVal
);
This uses Substring with only one argument (no length) to remove everything after the first hyphen up to the end of the string, and GetFileNameWithoutExtension to remove the .xml - this effectively turns anythinghere-2000_01_01-00_00_00.xml into 2000_01_01-00_00_00 ready for parsing
I could also have gone for a .Remove("last index of period") type thing but it does get a bit messy because you have to subtract the start Index of the hyphen etc
MJWill's comment about splitting on hyphen is also a good one - you could split then take the [1] and [2] indexes and join then back together for parsing..
Lastly don't forget that the file itself might have a created date that is already a good candidate for the date of creation rather than the filename (which might be mangled by humans) so long as it hasn't been transmitted somewhere and re-saved. Take a look at the FileInfo.CreationTime property for that - https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.8
First, we have to extract (match) the datetime part from a file name:
using System.Text.RegularExpressions;
...
// Aggravated task: dots and minuses within file's name
string source = #"bla-bla-bla-test.me-2000_01_02-10_12_14.xml";
string datetime = Regex.Match(
Path.GetFileNameWithoutExtension(source),
"[0-9]{4}_[0-9]{2}_[0-9]{2}-[0-9]{2}_[0-9]{2}_[0-9]{2}$").Value;
Then we can parse it
if (DateTime.TryParseExact(
datetime,
"yyyy_MM_dd-H_m_s",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal,
out DateTime result) {
// result is the parsed date
}
else {
// File doesn't contain valid date and time
}
I would suggest you to use regular expression assuming that your file name will be always following the same format you can do something like this:
var pattern = #"\d{4}_\d{2}_\d{2}-\d{2}_\d{2}_\d{2}";
var fileName = "test-2000_01_02-10_12_14.xml";
var match = new Regex(pattern);
var result = match.Match(fileName);
if (result.Success)
{
DateTime.TryParseExact(result.Value,
"yyyy_MM_dd-HH_mm_ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out DateTime dateVal);
}

What is the recommended way to insert current date in an Excel cell, as a constant?

I have been using the following code:
private void InsertDate(string dstCoordinates)
{
Range dstRange = worksheet.get_Range(dstCoordinates);
dstRange.Formula = "=TODAY()";
dstRange.Locked = true;
}
Which re-evaluates the date every time the spreadsheet file is opened.
But now I need the date to be evaluated once, when it is inserted in the cell. From that moment on, the value should remain constant.
That value seems to be stored internally as double.
Perhaps what I need is a function as follows:
string today = ExcelEvaluate("=TODAY()");
dstRange.value2 = today;
Is there such facility?
TIA
Like Corak wrote you can probably use ToOADate(). You need to set the cell format to date after it, so that it doesn't display as a double. The TODAY() function does that automatically.
Here's the code:
dstRange.Value2 = DateTime.Now.ToOADate();
dstRange.NumberFormat = "m/d/yyyy"

How can I format string values using a mask in a generic way?

Background: We have a system that receives data from another backend system. We handle the displaying of that data, and we have our own XML templates to control how certain things are displayed (i.e. we have our own column templates to dictate what the column headers are, etc.) One thing we would like to support is the ability to provide a mask for these column templates that would apply to the values coming from the backend. Below is a scenario that I'm having trouble with.
Problem: I can't seem to get a simple string format working. I'd like to format a STRING value of four digits (i.e. "1444") in a time format (i.e. "14:44"). I've tried:
String.Format("{0:00:00}", "1444")
Note the importance of the input being a STRING. If I supply an int value, the format will work. The reason I cannot use this is because all the data we receive from the backend is in string format, and we'd like for this to be generic (so casting isn't really an option).
By generic, I mean I'd like to specify a mask in our own XML templates, something like:
<MyColumnTemplate Id="MyColumn" Mask="00:00" />
and to use that mask in a string format call for string values? If the mask fails, we could just simply return the original value (as the String.Format() method already does by default).
Edit: To help clarify, here is a simplified version of what I'd like to be able to do in code:
string inputValue = "1444";
string maskValueFromXml = "00:00";
string mask = "{0:" + maskValueFromXml + "}";
string myDesiredEndResult = String.Format(mask, inputValue);
The thing is you are working string to string,since you ask for time and phone number they are all numbers then try this trick(if we can call it that :)):
string result = string.Format("{0:00:00}", int.Parse("1444"));
For phone number:
string result = string.Format("{0:000-000-0000}", int.Parse("1234560789"));
You can even place your desired masks in a dictionary for example and do this:
Dictionary<string, string> masks = new Dictionary<string, string>();
masks.Add("Phone", "{0:000-000-0000}");
masks.Add("Time", "{0:00:00}");
string test = "1234560789";
string result = string.Format(masks["Phone"], int.Parse(test));
Try with DateTime.TryParseExact, e.g:
DateTime dateEntered;
string input = "1444";
if (DateTime.TryParseExact(input, "HH:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dateEntered))
{
MessageBox.Show(dateEntered.ToString());
}
else
{
MessageBox.Show("You need to enter valid 24hr time");
}
After that, you can use string.Format, predefined formats on MSDN.

Read video information(date created)?

In window, i can get the date created of the video from properties(right click). I have a few idea on this but i dont know how to do it. 1. Get the video information directly from video(like in windows),
2. By extracting the video name to get the date created(The video's name is in date format, which is the time it created). And i also using taglib-sharp to get the video duration and resolution, but i cant find any sample code on how to get the video creation date.
Note: video name in date format - example, 20121119_125550.avi
Edit
Found this code and so far its working
string fileName = Server.MapPath("//video//20121119_125550.avi");
FileInfo fileInfo = new FileInfo(fileName);
DateTime creationTime = fileInfo.CreationTime;
Output: 2012/11/19 12:55:50
For the file's name, i will add another string in name. For example User1-20121119_125550.avi.avi, so it will get complicated after that.
If you can safely trust your filenames, you may be content with the following:
string file_name = "20121119_125550.avi";
string raw_date = file_name.Split('.')[0];
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyyMMdd_hhmmss";
DateTime result = DateTime.ParseExact(raw_date, format, provider);
Note: You'll likely need to add using System.Globalization; to any file you wish to use this in.
If you just want the date the file was created (What you see in Windows Explorer) you can just use:
string file_path = #"C:\20121119_125550.avi"; //Add the correct path
DateTime result = File.GetCreationTime(file_path);

C# times matching

I have written a small function in C# which isn't my main launguage so is coming across a little foreign to me.
public bool CheckForKey(string key)
{
string strKeyTime = Decode(key);
//valid key will be current time +- 5 minutes
string strTheTime = DateTime.Now.ToString("HH:mm:ss tt");
if (strKeyTime == strTheTime)
{
return true;
}
else
{
return false;
}
}
I need to alter this to allow for 5 minutes, so
if (strKeyTime == strTheTime)
needs to be
if (strKeyTime == strTheTime + or - 5 minutes)
my problem is matching the times as they are strings, perhaps convert key(original time) back to a date first and then do it, but I am pretty new to c#
If you convert (or keep) them both to DateTimes you can use TimeSpan:
TimeSpan delta = dateTime1 - dateTime2;
if (Math.Abs(delta.TotalMinutes) <= 5) { ... }
Look into using the DateTime.ParseExact (or any of the Parse... methods) to parse your strKeyTime, and then do something similar to the above.
To convert your sent string to the equivalent DateTime value, use the following code:
var keyDateTime = Convert.ToDateTime(strKeyTime);
var strTheTime = DateTime.Now
from here, you can use this value to compare with your original time value as the following:
if (keyDateTime == strTheTime || (keyDateTime > strTheTime && keyDateTime < strTheTime.AddMinutes(5))
{
return true;
}
the previous block of code will first check if we got an exact match, or the time sent to the method is between the original time and a time shift of additional 5 minutes.
that's it, if this is not what you need, let me know so I may update my answer for you, thanks.
-- if my answer is correct, don't forget to "Mark as answer".
"perhaps convert key(original time) back to a date first and then do it" sounds like a sound solution. I'd do it this way:
Convert both strings to DateTime instances.
Store difference in TimeSpan instance using DateTime.Subtract(DateTime value) ( http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx )
Check if TimeSpanInstance.TotalMinutes is less than or equal to 5.
The first step is something I can't really give you any advice on without information concerning how your string is formatted. However, once this is done, the rest should be easy.

Categories