Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:57:44+00:00 2026-06-12T23:57:44+00:00

I have here a string(an input from the user) for a file path. I

  • 0

I have here a string(an input from the user) for a file path. I checked the string so that it will qualify the criteria:

  • check for invalid characters for a file path
  • will not accept absolute path (\Sample\text.txt)

Invalid characters are:

< > : ” / \ | ? *

I have tried catching these invalid characters in catch clause. It work except for ‘\’. It will accept ‘C:\\Sample\text.txt’ which is an invalid file path.

The following examples should be invalid paths:

  • :\text.txt
  • :text.txt
  • \:text.txt
  • \text.txt
  • C:\\\text.txt

The ff. is an example of a valid path:

  • C:\text.txt

I have been through similar questions posted here but none of them seemed to solve my issue.

What would be the best way to do such check?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:57:45+00:00Added an answer on June 12, 2026 at 11:57 pm

    You may use Path.GetFullPath, it will throw an exception if the path is invalid. You can have method like:

    public static bool IsValidPath(string path)
    {
        try
        {
           path = path.Replace(@"\\", ":"); // to cancel out c:\\\\test.text
           string temp = Path.GetPathRoot(path); //For cases like: \text.txt
           if (temp.StartsWith(@"\"))
                return false;
           string pt = Path.GetFullPath(path);
        }
        catch //(Exception NotSupportedException) // catch specific exception here or not if you want
        {
            return false;
        }
        return true;
    }
    

    Sample code to test:

    List<string> list = new List<string>()
    {
        @":\text.txt",
        @":text.txt",
        @"\:text.txt",
        @"\text.txt",
        @"C:\\\text.txt",
        @"C:\text.txt",
    
    };
    
    foreach(string str in list)
    {
        Console.WriteLine("Path: {0} is Valid = {1}" ,str,IsValidPath(str));
    }
    

    Output:

    Path: :\text.txt is Valid = False
    Path: :text.txt is Valid = False
    Path: \:text.txt is Valid = False
    Path: \text.txt is Valid = False
    Path: C:\\\text.txt is Valid = False
    Path: C:\text.txt is Valid = True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string input from which I need to extract simple information, here
C# question here.. I have a UTF-8 string that is being interpreted by a
I have a program that load data from a file using std::ifstream and store
I have a program that takes a list of names from a file and
I have a program x.exe it waits for an input from user and reads
I have an application that writes its own stylesheet, based off user input. It
I have a method which requests external webservices and returns json here: public string
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
i have used libxml2.Here when i am getting an XML data as a string
Say I have file A, in middle of which have a tag string #INSERT_HERE#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.