Results 1 to 7 of 7

Thread: File Exists

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    File Exists

    Hello

    I have the following code:

    Code:
       If Dir("C:\Test.txt") <> "" Then
                TextBox1.Text = "File exists"
            Else
                TextBox1.Text = "File does not exist"
            End If
    However, I'd like to use this alongside a DataGridView.

    Basically, when the application runs, the user selects a folder for file storage. Then, in the DataGridView, the contents in each cell of Column 1 become the file name. In short, the contents of a cell, plus the selected folder for file storage, becomes a whole file path.

    I need to use the above code to get my application to check if the file exists, and if it does, it should place the text "File Exists" in column 2 of the DGV.

    I can't work out a way for the DGV to be "continually aware" if files have been created and so to update automatically without the user having to press buttons etc. As soon as the file exists, the correct text should be placed in Column2.

    Thanks for the help!

    M

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: File Exists

    For it to be aware of changes to your file system, the operating system would have to issue some sort of an alert which you are monitoring, or you would have to monitor it yourself. I am not aware of any standard trigger inside the operating system, so the easiest way would for you to regularly poll your file system.

    Pick a time period for your poll. Too fast will add a lot of overhead to your system and slow things down. Say 1 minute (or whatever time frame you pick). You would use a timer, and when it triggers, check your file system. This would need to continually run in the background to work.

  3. #3
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: File Exists

    Check out the BackgroundWorker Class

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: File Exists

    Do not use Dir, that's VB6 code.

    Use File.Exists instead. Add "Imports System.IO".
    Last edited by Chris001; Apr 17th, 2012 at 07:35 AM.

  5. #5
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: File Exists

    Quote Originally Posted by Chris001 View Post
    Do not use Dir, that's VB6 code.

    Use File.Exists instead. Add "Imports System.IO".
    Yes OP, Get with the Program

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: File Exists

    Quote Originally Posted by Chris001 View Post
    Do not use Dir, that's VB6 code.
    I don't know why MS didnt remove that. It is such utter trash even in VB6. The FindFirstFile API and its family are vastly superior. I stopped using Dir in VB6 in favour of those APIs.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: File Exists

    May above reason not to use dir

    vb Code:
    1. Dim f As String = "C:\file.txt"
    2. Dim flags As FileAttributes = File.GetAttributes(f)
    3. File.SetAttributes(f, FileAttributes.Normal)
    4. MsgBox(String.Format("File exist {0} {1}", Dir(f) <> Nothing, IO.File.Exists(f) <> Nothing))
    5. File.SetAttributes(f, FileAttributes.Hidden)
    6. MsgBox(String.Format("File exist {0} {1}", Dir(f) <> Nothing, IO.File.Exists(f) <> Nothing))

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width