accessing the program files directory

Just starting out? Need help? Post your questions and find answers here.
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

accessing the program files directory

Post by sirrab »

Hi All,

This has most likley been asked before and I have spent a hour trying to search for an answer. So Help please :)

How do I give my program write access to the program files dir so I can create a directory and add files to this directory.

I have tryed using request administrator mode with out any luck. Im running windows 7 but need it to work on vista too please.

Thank You
Craig
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: accessing the program files directory

Post by IdeasVacuum »

Could you not simply use an installer app? There is a free one delivered with Windows.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: accessing the program files directory

Post by sirrab »

IdeasVacuum wrote:Could you not simply use an installer app? There is a free one delivered with Windows.
I could but then I wouldn't learn how to do something new.
Craig
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: accessing the program files directory

Post by jesperbrannmark »

What installer app is included in windows?
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: accessing the program files directory

Post by sirrab »

jesperbrannmark wrote:What installer app is included in windows?
Good Point :)
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: accessing the program files directory

Post by Crusiatus Black »

I don't know about the default installer in Windows, but you could use the
fancy BytesSence Install Maker. Free and very nice, requires no scripting.

Written in PureBasic, I believe.

http://www.bytessence.com/bim.html


As for your original question, administrator rights should be enough to be
able to write to the Program Files directory.

This works for me:

Code: Select all

szProgramFiles.s = GetEnvironmentVariable("programfiles")
If(Right(szProgramFiles, 1) <> "\")
  szProgramFiles + "\"
EndIf 

szDirectory.s = szProgramFiles + "Testing\"

If(CreateDirectory(szDirectory))
  hFile = CreateFile(#PB_Any, szDirectory + "test.txt")
  If(hFile)
    Debug WriteString(hFile, "Hello World!!")
    CloseFile(hFile)
  EndIf 
EndIf 
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: accessing the program files directory

Post by sirrab »

Thanks Crusiatus Black,

Is what I needed. I could use that Installer,but always nice to be able to do something your self. Something I might need to know in the furture :)

Thanks
Craig
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: accessing the program files directory

Post by Inf0Byt3 »

Here's the code that I use in the install maker to re-launch the installer EXE and gain administrator rights:

http://www.purebasic.fr/english/viewtop ... shExecInfo

To actually gain the rights to write to Program Files, you'll need to explicitly right click your executable and select "Run as Administrator" from the context menu. That should work in theory.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: accessing the program files directory

Post by Danilo »

Inf0Byt3 wrote:Here's the code that I use in the install maker to re-launch the installer EXE and gain administrator rights:

http://www.purebasic.fr/english/viewtop ... shExecInfo

To actually gain the rights to write to Program Files, you'll need to explicitly right click your executable and select "Run as Administrator" from the context menu. That should work in theory.
It is the same way recommended by Microsoft. Small working example for self-elevation:

Code: Select all

If ProgramParameter(0)<>"-admin"
    ;
    ; main program
    ;
    MessageRequester("INFO","USER MODE")

    FileName.s = ProgramFilename()
    
    Info.SHELLEXECUTEINFO
    Info\cbSize = SizeOf(SHELLEXECUTEINFO)
    Info\lpVerb = @"runas"
    Info\lpFile = @FileName
    Info\lpParameters = @"-admin"
    Info\nShow = #SW_SHOW
    ShellExecuteEx_(@Info)
Else
    ; dialog for admin mode
    MessageRequester("INFO","ADMIN MODE")
EndIf
Post Reply