Question

I am converting a VB6 application to VB.NET. In the code I am converting, it seems that the developer found the applications path two separate ways. However, it appears that he expects the two methods to produce different results.

Simple question:

What is the difference between calling these two lines of code:

strAppDataPath = CreateObject("Shell.Application").
                             NameSpace(ssfLOCALAPPDATA).Self.Path

and

strAppDataPath = App.Path

Explanation:

This is the code in question:

strAppDataPath = CreateObject("Shell.Application").
                             NameSpace(ssfLOCALAPPDATA).Self.Path
strAppDataPath = strAppDataPath & "\DataFolder\"


If (Not objFileSystem.FileExists(strAppDataPath & strAppDataFile)) Then
    If (objFileSystem.FileExists(App.Path & strAppDataFile)) Then
        ...
    End If
End If

The application's path is appended with \DataFolder\, and stored in the String strAppDatapath.

We check if the file strAppDataFile does not exist in strAppDatapath. Followed by checking if the file does exist in App.Path.

The concept behind what is going on makes sense to me: If the file doesn't exist in the subfolder, and if the file exists in App.Path, then do .... What I do not understand is why they didn't use one of the method for finding the application's path exclusively.

Was it helpful?

Solution

They don't point to the same path.

  • App.Path: Path where the currently executing EXE/DLL resides.
  • CreateObject("Shell.Application").NameSpace(ssfLOCALAPPDATA).Self.Path: Will point to the local (non-roaming) app data user folder. Same as the value in (user) environment variable LOCALAPPDATA.

Since Windows XP, the roaming & local app data paths are preferred places to store user settings and files (instead of the probably rights-restricted app folder).

So what the code does, is:

  1. Look for strAppDataFile in %LOCALAPPDATA%\DataFolder\ (e.g. where "%LOCALAPPDATA%" could point to "C:\Users\UserName\AppData\Local\" on Windows Vista/7)
  2. If not found, look for the same file in the application folder.

Please note that preferably applications should create an application specific sub-directory in local/roaming app data, e.g. use something like:

strAppDataPath = strAppDataPath & "\" & App.ProductName & "\DataFolder\"
' NOTE: Make sure to set the "Product Name" entry in the version information
'       of the project settings

that could resolve to something like

"C:\Users\UserName\AppData\Local\My Application\DataFolder\"

OTHER TIPS

My guess is that when the program is installed per-machine, a per-user location under LocalAppData is used. However during development or when installed per-user or as a portable application the application folder is used.

Some programmers use something like the code in question, while others make the decision by comparing App.Path against the ssfPROGRAMFILES path to determine the environment. The latter is probably preferable for a number of reasons but in the degenerate case where only one instance of the program exists on a machine they're equivalent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top