Reputation: 117
I am trying to test if a file exists in the Virtual Store on my Windows 8.1 PC. If I go to Windows explorer, I can see it but when I use the following code, it is treating it as if the file does not exist.
Dim virtualFilePath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Local\VirtualStore\Windows\fsp.bin")
If System.IO.File.Exists(virtualFilePath) Then
Console.WriteLine("File exists in the virtual store")
End If
Where am I going wrong?
EDIT My error. I got the file path to the virtual store wrong by accidently including an extra 'local' directory. The following code works:
Dim virtualFilePath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VirtualStore\Windows\fsp.bin")
Upvotes: 1
Views: 757
Reputation:
The Problem is you are pointing to the wrong directory, since because
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)'<---- points to the
'<--- C:\Users\me\AppData\Local
when you combine it with "Local\VirtualStore\Windows\fsp.bin"
will lead to an unknown directory because it results in a path C:\Users\me\AppData\Local\Local\VirtualStore\Windows\fsp.bin
As you said an additional local is occur so you need to eliminate this, try the code as below
Dim virtualFilePath As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString & "\VirtualStore\Windows\fsp.bin"
If System.IO.File.Exists(virtualFilePath) Then
Console.WriteLine("File exists in the virtual store")
End If
Upvotes: 2
Reputation: 117
It turns out that the file path set to the virtual store was incorrect. After removing the extra 'local' directory, the above code does correctly indicate that the file exists.
Upvotes: 1
Reputation: 1
I usually use the FileInfo class, it works perfetly.
Dim f As New IO.FileInfo(Path)
If f.Exists Then
'do something
End If
Upvotes: 0