Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-27
10
Medium Priority
?
262 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
Comment
Question by:Richard Comito
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 900 total points
ID: 12427668
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:Richard Comito
ID: 12428090
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
ID: 12428200
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:wguerram
ID: 12428277
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
ID: 12428473
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:Richard Comito
ID: 12428629
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
ID: 12428642
You can choose to split points if you wish - just choose "assisted answer"
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12428653
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
ID: 12429551
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:Richard Comito
ID: 12430229
Just wanted to make sure like that I did the right thing.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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