Tech Off Post

Single Post Permalink

View Thread: C# Sting converstion
  • User profile image
    peoples

    W3bbo said:
    peoples said:
    *snip*

    For error handling, you're meant to use Try/Catch and Exceptions; The "On Error Do Something" in VB.NET is meant as a crutch for backwards compatibility with VB6 (i.e. never use it in new projects).

     

    To see if a file exists or not:

     

    try {

        FileStream fs = new FileStream("path to file");

    } catch(FileNotFoundException fex) {

    }

     

    ...of course, you should never use exceptions for normal program flow, it's much better to test first:

     

    try {

        if( File.Exists("path to file") ) {

            // do file operation here

        } else {

            // show error message to user

        }

    } catch(IOException ex) {

    }

     

    And as you're using C#, you might as well make use of the using() {} block to ensure your program doesn't shít itself if something bad happens in the file IO ops.

    Thanks