C# String Operations - Question

  • Thread starter Thread starter Rhonda Tipton
  • Start date Start date
R

Rhonda Tipton

Hello all.
Is there a command that can be used to change the string "New York
Collection Letters.pdf" to "New York Collection Letters_200509.pdf"? (right
or variation of substring)

Basically what I have been tasked to do is copy a file from one place to
another changing the name of the file at the same time which may sound easy,
but I am very new to .net. I have figured out how to copy a file in C#
and how to get the year and the month from the current date. I just can't
figure out how to insert the year and month at the end of the file name.

Any help is greatly appreciated.

Rhonda
 
Hi Rhonda,

Sure is, you can use String.Insert. To find the position to insert at use Path.GetFileNameWithoutExtension or subtract the length of Path.GetExtension.
 
Hi,

You could use code like the following

string filename = "New York Collection Letters.pdf";
string newFilename = string.Format("{0}_{1}{2}",
Path.GetFileNameWithoutExtension(filename),
DateTime.Now.ToString("yyyyMM"),
Path.GetExtension(filename));

I used string.Format, you could also just use straight string concatenation


Hope this helps
 
Back
Top