Using FileSystemObject to access all files in a Folder using Visual Studio 2005
Hello everyone,
I am looking to access all the files from a selected folder . The folder is selected through a folderbrowserdialog. My problem is using a filesystemobject in Visual Basic of Visual Studio 2005 to store the filename and path in a folder object. I have done the same using VB6 and it worked. However, I tried it with VS 2005 and it is giving me problems. I am clueless how to proceed. The following is my code in VB6.
Code:
If filesysObj.FolderExists(folderSel.Text) Then
Set folderObj = filesysObj.GetFolder(folderSel.Text)
For Each fileObj In folderObj.Files
file_name = fileObj.Name
firstpos = InStr(file_name, "fcs_")
secpos = InStr(file_name, "RegDump_")
thirdpos = InStr(file_name, "RSSI_")
If Not (firstpos <> 0 Or secpos <> 0 Or thirdpos <> 0) Then
If StrComp(Right(file_name, 4), ".txt") = 0 Then
whole_name = fileObj.ParentFolder + "\" + file_name
Set workbkObj = xlApp.Workbooks.Open(whole_name)
End If
End If
Next
End If
Now, I am trying to make changes to my Vb6 code so that it works with VB2005. Following is what I have done so far:
Code:
Dim folderObj As String
If My.Computer.FileSystem.DirectoryExists(folderSel.Text) Then
folderObj = System.Environment.GetFolderPath(folderSel.Text)
I am getting an error (InvalidCastException) on line, folderObj = System.Environment.GetFolderPath(folderSel.Text). The message I get is: Conversion from string "C:\tempfolder" (which is the path) to type 'Integer' is not valid.
I have been breaking my head over this for the last 3 days and do not know how to proceed since I am new to VB 2005 programming. Please help me. I would be grateful if someone could help me with the code based on my VB6 code. The migration tool on Visual Studio is not of any help to me.
Thanks,
vbadvt
Re: Using FileSystemObject to access all files in a Folder using Visual Studio 2005
I would offer two pieces of advice...
First...forget your VB6 code - .Net offers much better ways of handling files and directories. Conversion of VB6 code line-by-line will rarely give you the best solution in the .Net world.
Second... search this board for "access all files in a folder" and you'll find examples of how to do it in .Net.