+ Reply to Thread
Results 1 to 7 of 7

Locating a user's Program Files

Hybrid View

  1. #1
    cush
    Guest

    Locating a user's Program Files

    I am able to locate a users desktop with the following code:

    Set WSHShell = CreateObject("WScript.Shell")
    sPath = WSHShell.specialfolders("Desktop")

    I need something similar to locate the Program Files, which are not always
    located at C:\Program Files

    I tried:
    Set WSHShell = CreateObject("WScript.Shell")
    sPath = WSHShell.SpecialFolders("Program Files")

    both with and without the space, but it only turned up
    an empty string.

    sPath = WSHShell.SpecialFolders("Programs")
    returns : \Start Menu\Programs


    Any clues?
    Also, where can I find the object model for this?


  2. #2
    Dave Peterson
    Guest

    Re: Locating a user's Program Files

    This worked ok for me in WinXP (not sure it works in all versions of windows).

    Option Explicit
    Sub testme01()
    MsgBox Environ("programfiles")
    End Sub

    But I searched *scripting* newsgroups and found something that was close to
    this:

    Option Explicit
    Sub testme02()

    Dim sPath As String
    Dim WSHShell As Object
    Set WSHShell = CreateObject("WScript.Shell")

    sPath = WSHShell.environment("Process")("ProgramFiles")

    If sPath = "" Then
    sPath = WSHShell.RegRead _
    ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir")
    End If

    MsgBox sPath

    End Sub

    It looks like it tries to use the environment variable. If it doesn't find it,
    it goes and reads the registry.



    cush wrote:
    >
    > I am able to locate a users desktop with the following code:
    >
    > Set WSHShell = CreateObject("WScript.Shell")
    > sPath = WSHShell.specialfolders("Desktop")
    >
    > I need something similar to locate the Program Files, which are not always
    > located at C:\Program Files
    >
    > I tried:
    > Set WSHShell = CreateObject("WScript.Shell")
    > sPath = WSHShell.SpecialFolders("Program Files")
    >
    > both with and without the space, but it only turned up
    > an empty string.
    >
    > sPath = WSHShell.SpecialFolders("Programs")
    > returns : \Start Menu\Programs
    >
    > Any clues?
    > Also, where can I find the object model for this?


    --

    Dave Peterson

  3. #3
    Harald Staff
    Guest

    Re: Locating a user's Program Files

    "Dave Peterson" <[email protected]> skrev i melding
    news:[email protected]...
    > This worked ok for me in WinXP (not sure it works in all versions of

    windows).
    >
    > Option Explicit
    > Sub testme01()
    > MsgBox Environ("programfiles")
    > End Sub


    "Environ" is very limited in the Windows 9x family (95, 98, ME), but works
    fine in the NT family (NT, 2000, XP). If this is a problem, Randy Birch has
    code for this (and almost everything else) at
    http://vbnet.mvps.org/code/browse/csidl.htm
    it is VB6, so the Form parts has to be adjusted

    Best wishes Harald



  4. #4
    Dave Peterson
    Guest

    Re: Locating a user's Program Files

    Do you know if this would work in the 9x series?

    If sPath = "" Then
    sPath = WSHShell.RegRead _
    ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir")
    End If

    Thanks,



    Harald Staff wrote:
    >
    > "Dave Peterson" <[email protected]> skrev i melding
    > news:[email protected]...
    > > This worked ok for me in WinXP (not sure it works in all versions of

    > windows).
    > >
    > > Option Explicit
    > > Sub testme01()
    > > MsgBox Environ("programfiles")
    > > End Sub

    >
    > "Environ" is very limited in the Windows 9x family (95, 98, ME), but works
    > fine in the NT family (NT, 2000, XP). If this is a problem, Randy Birch has
    > code for this (and almost everything else) at
    > http://vbnet.mvps.org/code/browse/csidl.htm
    > it is VB6, so the Form parts has to be adjusted
    >
    > Best wishes Harald


    --

    Dave Peterson

  5. #5
    Peter T
    Guest

    Re: Locating a user's Program Files

    Hi Dave,

    > Do you know if this would work in the 9x series?
    >
    > If sPath = "" Then
    > sPath = WSHShell.RegRead _
    > ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir")
    > End If


    Yes, that worked for me in W98SE when I ran your testme02 as posted earlier.

    sPath = WSHShell.environment("Process")("ProgramFiles")
    and
    MsgBox Environ("programfiles")
    both returned empty strings but without error.

    FWIW, I notice that besides "ProgramFilesDir" I also have
    "ProgramFilesPath", adjacent in the registry. Both show same path, as I
    would expect but wonder why the two keys.

    Regards,
    Peter T



  6. #6
    Dave Peterson
    Guest

    Re: Locating a user's Program Files

    I use winXP.

    For me,
    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir
    contained a value of: C:\program files

    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesPath
    contained a value of: %ProgramFiles%

    which looks a lot like a way to refer to an environment variable.

    Like using Windows start button|run
    and typing:
    %temp%
    to get to the user's temp folder
    %programfiles%
    took me to my c:\program files folder.





    Peter T wrote:
    >
    > Hi Dave,
    >
    > > Do you know if this would work in the 9x series?
    > >
    > > If sPath = "" Then
    > > sPath = WSHShell.RegRead _
    > > ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir")
    > > End If

    >
    > Yes, that worked for me in W98SE when I ran your testme02 as posted earlier.
    >
    > sPath = WSHShell.environment("Process")("ProgramFiles")
    > and
    > MsgBox Environ("programfiles")
    > both returned empty strings but without error.
    >
    > FWIW, I notice that besides "ProgramFilesDir" I also have
    > "ProgramFilesPath", adjacent in the registry. Both show same path, as I
    > would expect but wonder why the two keys.
    >
    > Regards,
    > Peter T


    --

    Dave Peterson

  7. #7
    Jim Cone
    Guest

    Re: Locating a user's Program Files

    Absolutely essential download...
    http://msdn.microsoft.com/library/de...ist/webdev.asp
    Microsoft Windows Script Downloads

    Jim Cone
    San Francisco, USA


    "cush" <[email protected]> wrote in message
    news:[email protected]...
    I am able to locate a users desktop with the following code:

    Set WSHShell = CreateObject("WScript.Shell")
    sPath = WSHShell.specialfolders("Desktop")
    I need something similar to locate the Program Files, which are not always
    located at C:\Program Files
    I tried:
    Set WSHShell = CreateObject("WScript.Shell")
    sPath = WSHShell.SpecialFolders("Program Files")
    both with and without the space, but it only turned up
    an empty string.
    sPath = WSHShell.SpecialFolders("Programs")
    returns : \Start Menu\Programs

    Any clues?
    Also, where can I find the object model for this?


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1