C# Question: File Names – Education Career Blog

I have a file called o.FileName it has a unwanted “.crash” at the end of it, how can I remove it? I tryed “o.FileName = o.FileName – “.crash”;” it didn’t work because it was a string

Edit: What I want to do is open a file with a OpenFileDialog then if it has a .crash or a .crash.crash at the end of it remove it and save it

,

Was the ‘o.’ the object reference?:

o.Filename = o.Filename.Replace(".crash", "");

otherwise:

filename = filename.Replace(".crash", "");

To actually rename the physical file:

File.Move(originalFilename, renamedFilename);

,

Check that the string actually ends with “.crash” using the System.String.EndsWith() method. Drop the trailing “.crash” using System.String.Replace().

if(o.FileName.EndsWith(".crash"))
    o.Filename = o.Filename.Replace(".crash", "");

Leave a Comment