tags:

views:

154

answers:

2

I am trying to retrieve file details from files in the Program Files folder.
I receive an error while trying to retrieve file details.

Dim sFileName As String = ("%ProgramFiles%\Windows Defender\MSASCui.exe")
Dim Info As FileVersionInfo
Info = FileVersionInfo.GetVersionInfo(sFileName)
Msgbox(Info.ProductName.ToString())

I receive file not found error at 3rd line.
But if I change the path from "%ProgramFiles%\Windows Defender\MSASCui.exe" to "c:\Program Files\Windows Defender\MSASCui.exe" it works fine.

What should I do if I want to retrieve file details from the path which includes a "%" character?

+3  A: 

I haven't a Windows machine handy to test, but try expanding the environment variable first:

sFileName = Environment.ExpandEnvironmentVariables(sFileName)
Info = FileVersionInfo.GetVersionInfo(sFileName)
Adam Bernier
+3  A: 

You need to use an environment variable:

Dim programFiles As String = Environment.GetEnvironmentVariable("ProgramFiles")
Dim sFileName As String = Path.Combine(programFiles, "Windows Defender\MSASCui.exe")
Darin Dimitrov
+1 for `Path.Combine`.
Jim H.