PDA

View Full Version : Prevent saving files to System folders



ScottBrady
10th August 2011, 22:07
Hi All,

I am creating a dialog based program that requires the user to save a text file to the hard drive.

I have no problem saving my text file.

My question is: Is there a standard way of preventing the user from saving to any system folders like C:\ or C:\Program Files on a windows system.

Kind Regards

Scott

SixDegrees
10th August 2011, 22:33
You can probably intercept the directoryChanged signal, or whatever it's called, in an extension of the file dialog, then put up a warning and redirect away from the directories you don't want people to use.

But this sort of thing is better addressed by the user or system administrator through file and directory permissions settings. In general, I don't like programs that circumvent my choices about where to put files.

ScottBrady
11th August 2011, 13:08
My problem is that if I try to save the txt file to the users home directory everything saves fine.

If I try to save the file to a location other that the users home directory (for eg. c:\Program Files\), the file is not saved to where I directed it. Instead it is saved to the "Virtual Store" ie (C:\Users\UsersName\AppData\Local\VirtualStore\Pro gram Files)

I know that this is related to the fact that the user does not have the permissions to write to this location. Has anybody experienced this before, and what steps did you take to overcome this.

Thanks in Advance

Scott

wysota
11th August 2011, 13:24
Check the permissions before opening the file.

ChrisW67
12th August 2011, 07:38
Put a manifest into your Windows executable. This action will disable file system and registry virtualisation and attempts to write to prohibited areas should fail on Vista/Win 7.

ScottBrady
12th August 2011, 09:54
Hi Chris,

Do you know of anywhere that there are some resource on the web where I can investigate how to do this, as I have never used a manifest file.

Thanks

Scott

wysota
12th August 2011, 11:50
I'm sure MSDN is the right place to look for it.

ChrisW67
13th August 2011, 07:00
You use the RC_FILE variable in your PRO file to name a Windows resource file to include into your executable (you need that for the Windows desktop icon anyway). That resource file, in turn, drags in a manifest file. Something like:

myprog.pro:


RC_FILE = myprog.rc

myprog.rc:


...
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "myprog.exe.manifest"
...

myprog.exe.manifest:


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86"
name="com.example.myprog" type="win32" />
<description>My Prog</description>
<dependency />
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<!-- padding to eight-byte multiple file size including BOM -->
<!-- padding 123 -->
</assembly>


Your manifest may vary, but the one above is typical for user apps. I am not sure if the file size requirement still exists (it avoided a bug).

ScottBrady
13th August 2011, 15:19
Perfect !

Thanks Chris.

Exactly what I was looking for.

Kind Regards and many thanks.

Scott