C# Replace Function Error

Posted on 2007-07-31
Last Modified: 2008-01-09
string strFileName = "";
// retrieve the name of the file that is posted.
strFileName = oFile.PostedFile.FileName;
strFileName = Path.GetFileName(strFileName);

I am getting the following error at this statement --------- strFileName.Replace(":","_")
error: 'strFileName.Replace' does not exist

I am trying to achieve the following. If the strFileName consists of a COLON(:) charater, i wish to replace it by UNDERSCORE(_). Since i cannot save any file with a COLON in its name.

Also please suggest a function which checks for all invalid characters in a new file save operation.

Question by:TECH_NET
    LVL 33

    Expert Comment

    try this

    strFileName = strFileName.Replace(":", "_");
    LVL 22

    Expert Comment

    Assuming you're on 2.0 (not 1.1), the following should strip out all invalid characters (replacing with an underscore):

                foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                    strFileName = strFileName.Replace(c, '_');

    Author Comment

    I am using 1.1 and the error is thrown at strFileName = strFileName.Replace(":", "_");
    LVL 7

    Accepted Solution

    In .Net 1.1:

    strFileName = strFileName.Replace(Path.AltDirectorySeparatorChar, '_');
    strFileName = strFileName.Replace(Path.DirectorySeparatorChar, '_');
    strFileName = strFileName.Replace(Path.PathSeparator, '_');
    strFileName = strFileName.Replace(Path.VolumeSeparatorChar, '_');
    foreach (char c in Path.InvalidPathChars)
          strFileName = strFileName.Replace(c, '_');

    Use chars (surrounded by 'apostrophes', not strings surrounded by "quotes"). If strFileName.Replace is throwing an error, you are changing strFileName from a string to something else; string.Replace has been in .Net since 1.0.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now