I'm currently trying to add a DateTime
stamp, a prefix and a unique number to a file name. My desired output is:
\ParentDirectory\Sub Directory\Another Sub Directory\Prefix- Unique Number - 11 29 2016 2 07 30 PM.xlsx
Prefix
and Unique Number
above will be passed into the function. I'm using the following method to achieve this:
public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
return string.Concat(
Path.GetFullPath(fileName),
Path.Combine(prefix + " - " + uniqueNumber + " - "),
Path.GetFileNameWithoutExtension(fileName),
DateTime.Now.ToString()
.Replace("/", " ")
.Replace(":", " ")
.Trim(),
Path.GetExtension(fileName)
);
}
I call the above method as:
string fileName = @"\\ParentDirectory\Sub Directory\Another Sub Directory\MyFile.xlsx";
string adjustedFileName = fileName.AppendDateTimeToFileName("Shipping Note", "0254900");
The output I receive is as follows:
\ParentDirectory\Sub Directory\Another Sub Directory\Shipping Note -\0254900 - 11 29 2016 2 08 10 PM
As you can see in the above output the string is incorrect, firstly I get an extra -\
and the file extension isn't coming through either. Can someone tell me where I'm going wrong please.