Solved

How do I confirm that a file is really there before I look at it with FileOpen.

Posted on 2004-10-27
231 Views
Last Modified: 2012-05-05
I am using the FileOpen object to pull text from a file.  I want to make sure that before I hand it over to the object that the file is really there.  I want to be able to kick up a error message for the end user that will not take them out of the program.

here is the code that I am using.

            FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
            While Not EOF(1)
                txtLine = txtLine & vbCrLf & LineInput(1)
            End While
            FileClose(1)

Thanks.
0
Question by:GabicusC
    10 Comments
     
    LVL 10

    Accepted Solution

    by:
    Hi GabicusC,
    Well, this should work

    Imports System.IO

    If File.Exists("c:\somefile.txt") Then
    'code
    Else
    'code  
    End If

    Hope this helps.

    Cheers!
    0
     

    Author Comment

    by:GabicusC
    I hope this is not too much of a newbie question but when I input:
    imports System.IO  I get a "syntax Error"

    and then on the file I receive a "Name file is not declared".

    This is the way I entered in the code.

            If Me.lstEmailType.SelectedItem.ToString() = "LA Stage Tix" Then
            Else
                imports System.IO
                If File.Exists(Me.lstEmailType.SelectedItem.ToString() ) Then
                    FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
                    While Not EOF(1)
                        txtLine = txtLine & vbCrLf & LineInput(1)
                    End While
                    FileClose(1)
                Else
                    MsgBox("The file you have entered does not exists.  Please check your path and then try agian.")
                    Me.txtEmailLocation.Focus()
                    GoTo lastline
                End If

            End If
    0
     
    LVL 10

    Expert Comment

    by:NetworkArchitek
    Well, the "imports" statement just allows you to use that namespace. You want to invoke that command before any class declarations. Other than that your code is fine, but you can't "import" inside an else clause like that. If you have ever used C or any language like that all you are doing is essentially settings your "includes."
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    Enclose the rountine in a Try - Catch
    This way the program won't kick the user

    Try
          FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
                While Not EOF(1)
                    txtLine = txtLine & vbCrLf & LineInput(1)
                End While
    catch ex as exception
        msgbox ex.Message
    finally
           FileClose(1)
    end try
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    NetworkArchitek got it right - the imports system.io should be the very first line in your file. Everything else looks good at first glance.
    0
     

    Author Comment

    by:GabicusC
    Well this is a tough one.  NetworkArchitek gave me the answer, but toddhd just made it a little more clear. How do I work the accepting on this?

    Thanks for all your help.
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    You can choose to split points if you wish - just choose "assisted answer"
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    I consider you should give the points to NetworkArchitek

    Since He assummed you knew where to write the statement imports system.io and He Answered your Question.
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    I thought I clearly indicated that NetworkArchitek had given the correct answer. Also, I did not "assume" that GabicusC knew where to put the Imports statement - if he did, he would have done so in the first place. I just tried to clarify the proper action to take to minimize his steps to correct the problem. That's why the assisted answer exists. But whatever - like Drew Carey says on "Who's line is it anyway":

    "Everything is made up, and the points don't count anyway" :)

    So credit it however you like, I don't need the points. I just wanted to help.
    0
     

    Author Comment

    by:GabicusC
    Just wanted to make sure like that I did the right thing.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    913 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

    17 Experts available now in Live!

    Get 1:1 Help Now