|
-
Apr 17th, 2012, 06:27 AM
#1
Thread Starter
Addicted Member
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
-
Apr 17th, 2012, 06:38 AM
#2
Fanatic Member
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.
-
Apr 17th, 2012, 07:13 AM
#3
Re: File Exists
Check out the BackgroundWorker Class
-
Apr 17th, 2012, 07:31 AM
#4
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.
-
Apr 17th, 2012, 08:09 AM
#5
Re: File Exists
 Originally Posted by Chris001
Do not use Dir, that's VB6 code.
Use File.Exists instead. Add "Imports System.IO".
Yes OP, Get with the Program
-
Apr 17th, 2012, 05:00 PM
#6
Re: File Exists
 Originally Posted by Chris001
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.
-
Apr 17th, 2012, 06:31 PM
#7
Re: File Exists
May above reason not to use dir
vb Code:
Dim f As String = "C:\file.txt" Dim flags As FileAttributes = File.GetAttributes(f) File.SetAttributes(f, FileAttributes.Normal) MsgBox(String.Format("File exist {0} {1}", Dir(f) <> Nothing, IO.File.Exists(f) <> Nothing)) File.SetAttributes(f, FileAttributes.Hidden) 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|