How I can get the path of the programs folder?

Just starting out? Need help? Post your questions and find answers here.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

How I can get the path of the programs folder?

Post by Armoured »

Hi
How I can get the path of the program folder? :?
There is an Env variable for this path? :?

P.S.
I have WinXP

Thanks.
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: How I can get the path of the programs folder?

Post by Randy Walker »

Every windows since '95 has used C:\Program Files as the default location for this directory. Other than placing on another (boot) drive I don't think it can go anywhere else.

I'm lazy and I don't like fiddling with registry stuff (even to just read) so I would take the easy way out and use an if then approach.

Code: Select all

If Filesize("C:\Program Files") = -2
  path$="C:\Program Files"
ElseIf Filesize("D:\Program Files") = -2
  path$="D:\Program Files"
ElseIf Filesize("E:\Program Files") = -2
  path$="E:\Program Files"
;etc.
Endif
EDIT - You might want to add a backslash to your search string and path$ depending on your needs.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Re: How I can get the path of the programs folder?

Post by Blade »

Randy Walker wrote: I'm lazy and I don't like fiddling with registry stuff (even to just read) so I would take the easy way out and use an if then approach.
Non-english users (PB has many of them) will thank you for sure! 8)
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: How I can get the path of the programs folder?

Post by Randy Walker »

To answer your second question....

I am certain the path would be in the registry because Windows does allow D: or E: or F: etc to be your boot drive and so Windows has to keep record of this somewhere.

If you really want it, open regedit and search for ":\Program Files" without the quote marks. This will help you to locate the path to the KEY. Further examples here in the forum will show you how to read the registry. I do read it when I absolutely have to but I just prefer to leave it alone. This is an instance where I simply would not bother with the regisrty.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You can use this:

Code: Select all

ProgramFiles$ = Space(#MAX_PATH)
GetEnvironmentVariable_("PROGRAMFILES", ProgramFiles$, #MAX_PATH)
Debug ProgramFiles$
@Randy: so you're one of these programmers who hardcode the program files dir path? ("C:\Archivos de programa" here) tsk, tsk... :lol:
El_Choni
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

El_Choni wrote: @Randy: so you're one of these programmers...
Hold on one ding dang minute. Where in world did you ever get the notion that I was a programmer. A novice hacker at best maybe and I don't mean that in the typical misnomer (cracker) fashion. I understand a little bit and hack at it like hell until I get it to do something close to what I hope for. I consider programmers to be deeply versed individuals in the science and although I consider myself well versed in some respects, I have never ever refered to myself as a programmer.

The sample I gave was cut and paste, thrown together without much thought and left that way because of it's clarity. I could have cut it down and packed strings and substrings into variables but why bother when cut and past is easier?

Anyway, you came up with a much cleaner solution. New to me because anything in 32 bit APIs are all new to me. Catching up slowly though.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

@ El_Choni
Speaking of clarity. There is nothing a newcomer would find obscure in my code sample. Your's... well been here a while and never ran into #MAX_PATH before. Is this a PureBasic constant or one that you created but left out any assignment in your sample?

EDIT - catagory says beginners... can't believe this guy was laughing at me :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

(I wasn't laughing at you, I just put that emoticon to make clear I was kidding, not flaming you)

#MAX_PATH is a Windows constant, equal to 260. It represents the maximum number of characters that the string representation of a path can have. So it's often used when allocating memory to call a function which returns a path. You can use 1024 if you wish.

If you code programs, I would say you're a programmer. If I'm wrong, then ok, you're not a programmer. Neither am I. I am a tea biscuit. :D
El_Choni
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Hey Randy,

Taking little digs at each other is a European thing.

I know from past experience that this is something Americans aren't used too, so I hope you don't take any offence at El_Choni's comments.

It's a sign of friendliness, like a hearty slap on the back.

:)
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

El_Choni wrote:(I wasn't laughing at you, I just put that emoticon to make clear I was kidding, not flaming you)
Now what? You're saying I not worhty of flaming? :shock:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

GedB wrote:I know from past experience that this is something Americans aren't used too, so I hope you don't take any offence at El_Choni's comments.
Yes I caught that. I was joking as well... look up to see my smiley :wink:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: How I can get the path of the programs folder?

Post by GPI »

Randy Walker wrote:Every windows since '95 has used C:\Program Files as the default location for this directory.

Code: Select all

If Filesize("C:\Program Files") = -2
  path$="C:\Program Files"
ElseIf Filesize("D:\Program Files") = -2
  path$="D:\Program Files"
ElseIf Filesize("E:\Program Files") = -2
  path$="E:\Program Files"
;etc.
Endif
.
Congratulation. With this code, you faild to find my Program files-folder. Because i have a german system and this folder is called "C:\Programme". And when you have installed more than on Windows in diffrent partitions...

Very Bad code.

One of best methode is:

Code: Select all

Procedure.s Reg_GetValue(topKey, sKeyName.s, sValueName.s, ComputerName.s) 
  lpData.s 
  GetValue.s =""
  
  If Left(sKeyName, 1) = "" 
    sKeyName = Right(sKeyName, Len(sKeyName) - 1) 
  EndIf 
  
  If ComputerName = "" 
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  Else 
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry) 
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey) 
  EndIf 
  
  If GetHandle = #ERROR_SUCCESS 
    lpcbData = 255 
    lpData = Space(255) 
    
    GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData) 
    
    If GetHandle = #ERROR_SUCCESS 
      Select lType 
        Case #REG_SZ 
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData) 
          
          If GetHandle = 0 
            GetValue = Left(lpData, lpcbData - 1) 
          Else 
            GetValue = "" 
          EndIf 
          
        Case #REG_DWORD 
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lpType, @lpDataDWORD, @lpcbData) 
          
          If GetHandle = 0 
            GetValue = Str(lpDataDWORD) 
          Else 
            GetValue = "0" 
          EndIf 
          
      EndSelect 
    EndIf 
  EndIf 
  RegCloseKey_(hKey) 
  ProcedureReturn GetValue 
EndProcedure 
Procedure.s GetProgramFilesFolder()
  Type = $2
  location$=Reg_GetValue(#HKEY_LOCAL_MACHINE ,"SOFTWARE\Microsoft\Windows\CurrentVersion","ProgramFilesDir","")
  ProcedureReturn location$
EndProcedure

Debug GetProgramFilesFolder()
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

@ El_Choni

I mean no offense here either. When I am in the beginner forum I make efforts to post self explanitary code. Not always possible and sometimes I forget where I am... how I got here... what day it is... you know :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: How I can get the path of the programs folder?

Post by Randy Walker »

GPI wrote:Congratulation. With this code, you faild to find my Program files-folder. Because i have a german system and this folder is called "C:\Programme". And when you have installed more than on Windows in diffrent partitions...

Very Bad code.

Now we are in the presence of a true programmer and we have confirmation... I AM NOT A PROGRAMMER :D

Who'd of thought anyone would install Windows in some foreign language?
(Ignorence spilling out all over me here :) )

Thanks GPI!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: How I can get the path of the programs folder?

Post by gnozal »

Randy Walker wrote:Who'd of thought anyone would install Windows in some foreign language?
English is a foreign language for most people in the world :twisted:
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply