Problem with RegEx.Replace and trying to change a filename

Posted by fraXis on Stack Overflow See other posts from Stack Overflow or by fraXis
Published on 2010-05-20T22:43:24Z Indexed on 2010/05/20 22:50 UTC
Read the original article Hit count: 399

Filed under:
|
|
|

Hello,

I am having a strange problem and I can't seem to figure it out.

My filename is something like this:

DER 1513016-3.020F.NCF.

I want to be able to change it to:

DER 1513016-3.020H.NCF

Sometimes the filename can be this as well:

DER 1513016-3.020F_NEW.NCF

which would change to:

DER 1513016-3.020H_NEW.NCF



This is my code to do this:

        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "Open";
        fDialog.Filter =
           "NCF files (*.ncf)|*.ncf|All files (*.*)|*.*";
        fDialog.InitialDirectory = @"C:\Program Files\";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {


            string newfilename;
            string fileext = Path.GetExtension(fDialog.FileName);


            newfilename = Regex.Replace(fDialog.FileName, "F.NCF", "H.NCF");
            newfilename = Regex.Replace(fDialog.FileName, "F_NEW.NCF", "H_NEW.NCF");
   } 



This is where things get wierd. The way the code works now, it will NOT change the filename to DER 1513016-3.020H.NCF

If I comment out this line of code:

//newfilename = Regex.Replace(fDialog.FileName, "F_NEW.NCF", "H_NEW.NCF");

it will work fine and the file will now become: DER 1513016-3.020H.NCF



However, if I uncomment that line of code, the filename will not change to DER 1513016-3.020H.NCF. It will stay as DER 1513016-3.020F.NCF.

Why is that line of code causing the routine to not change the filename?

Thanks,

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex