Opening Folder if file exists in folder
Having difficulty with the module below failing on line 21. It is telling me:
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
Basically what it is designed to do is look for a directory, if that exists then look for a file in that directory. If both = True then it should open the folder to browse the files. I believe the issue is with the "Program Files" being 2 words since the script runs using a root path, but I'm not sure how to format the path correctly to make it work?! Ideas? :confused:
Code:
Imports System.IO
Module Startup
Sub Main()
Dim RPath As String
Dim RedunOff As String
RPath = "C:\Program Files\Directory\Data\Configuration"
RedunOff = "C:\Program Files\Directory\Data\Configuration\Random.xml"
If Directory.Exists(RPath) = True Then
If File.Exists(RedunOff) = True Then
Dim objShell = CreateObject("Wscript.Shell")
objShell.run(RPath)
End If
End If
End Sub
End Module
Re: Opening Folder if file exists in folder
First up, if you want to get the path to the Program Files folder, or any other standard folder for that matter, then you should be using Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories. You can then use IO.Path.Combine or My.Computer.FileSystem.CombinePath to append a relative folder or file path. If you then want to open a file or folder, you should be calling Process.Start. If you pass a folder path then it will open that folder in Windows Explorer.
Re: Opening Folder if file exists in folder
try this:
vb Code:
Dim RPath As String = "C:\Program Files\Directory\Data\Configuration"
Dim RedunOff As String = "C:\Program Files\Directory\Data\Configuration\Random.xml"
If IO.Directory.Exists(RPath) AndAlso IO.File.Exists(RedunOff) Then
Process.Start(RPath)
End If
Re: Opening Folder if file exists in folder
By the way, there's not much point checking for the existence of the folder and the file. If the file doesn't exist then it doesn't matter whether the folder exists because you're not using it anyway and if the file exists then you know for a fact that the folder exists too.
Re: Opening Folder if file exists in folder
Quote:
Originally Posted by
jmcilhinney
By the way, there's not much point checking for the existence of the folder and the file. If the file doesn't exist then it doesn't matter whether the folder exists because you're not using it anyway and if the file exists then you know for a fact that the folder exists too.
oh yeah...:D didn't think of that...