Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i am creating a program that loads settings, i want to know what is the best/ easy way to restrict users from entering an invalid path to the textbox
Posted
Updated 27-Apr-23 21:15pm
Comments
Sinisa Hajnal 23-Oct-14 8:34am    
Why couldn't you simply google this. There are plenty of examples.
Member 11166202 23-Oct-14 8:37am    
i have and i am still goolgling it, haven't found anything!!
Sinisa Hajnal 23-Oct-14 8:53am    
You have two way to solve it in my solution. Take a look.
Sergey Alexandrovich Kryukov 23-Oct-14 17:03pm    
It can be understood in different ways. What do you want to know? If the directory exists or if the user can create a directory with a given name?
Also, it may not mean that the directory if "valid". Valid for what operation? For example, a directory may exist but be not accessible to a give user (not visible, not allowed to see the content, read-only, and so on).
—SA
Member 11166202 24-Oct-14 2:49am    
for instance if a user enters / temp\ "2\1 instead of C:\temp\ the program must be able to tell the user if he entered an invalid path or not! i hope i gave you clarity.

Try:
C#
if (!Directory.Exists(path))
   {
   // Report problem
   ...
   }
 
Share this answer
 
You cannot restrict users, but you can warn them they entered invalid path. Actually, that is wrong, you can restrict users by using Browser Dialog[^] without allowing writing into the Textbox

The way to check for invalid path (if you don't want to use BrowserDialog)
C#
if (System.IO.Directory.Exists() ) {
    MsgBox("You entered invalid folder path!", MsgBoxStyle.Exclamation);

}


Put the check above in textbox validating event.

If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
File exists
C#
if (!File.Exists(path))
   {
   // Report problem
   ...
   }

Folder exists
C#
if (!Directory.Exists(path))
   {
   // Report problem
   ...
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900