• Advertisement
Sign in to follow this  

[.net] How to validate file path and check file type?

This topic is 3679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use Visual C++ and I want to load a text file to a textBox. How I can check if the file is true text? Not a binary? I have used the openFileDialog filter and it is working without problem. But I also must handle the user input filename via the textBox. 1. I must validate the user file name input. I think, users are get usual with mistakes. So let say, they input wrong path like this: C::\\\...... It is truly not valid file path.. Does the File class has a method to validate the user input? If not, how can I validate it? 2. I must ensure that user input file is a text file, not binary or other type..How to know if it is a text file / non-text file? Thank you very much.

Share this post


Link to post
Share on other sites
Advertisement
System.IO.File.Exists(path) will work for the first one. You can also use methods in System.IO.Path like System.IO.Path.GetDirectory(). You can also get the extension that way.

To find out if the file is a plain text file, you would probably have to look at the header or something. Look more into the System.IO.File type, it may have what you need.

Share this post


Link to post
Share on other sites
For accessing file info you can look into the System.IO.FileInfo object class. Once created for a given path there is data you can access about the file itself.

-Devin

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement