[c++/cli] UnauthorizedAccessException while opening the exe file

I just finished my application - it works properly when im trying to open it on different computer im suffering this problem (after opening the exe file):


System.UnauthorizedAccessException: Access to file is denied
'C:\Program Files\BBO Generator\database.dat'.
in System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
in System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32
bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String
msgPath, Boolean bFromProxy)

etc...


the program works with several files, based on StreamWriter/Reader (Writer and Reader are declared in different method of my class):
1
2
3
StreamWriter^ sw = gcnew StreamWriter("database.dat");
//....
StreamReader^ sr = gcnew StreamReader("database.dat");


I looked thru my .NET book, from which i'm learning atm and found something about defining permissions. Maybe thats the key to the problem.

I haven't tried it out yet, but is it any difference between code above and this: (?)
 
StreamReader^ sr = gcnew StreamReader(File::Open("database.data",FileMode::Create,FileAccess::ReadWrite));

I searched for it in google, found some issues concerning that exception above, but none from them were related with mine, i think...



Anyone got some ideas for me?
thx in advance,
JK
Last edited on
why didnt u try it out?^^...
closed account (z05DSL3A)
System.UnauthorizedAccessException: Access to file is denied

Sounds like a file permision problem, the Program Files directory does tend to restrict the modification of files under it.
that happend on VISTA, to be honest i have no idea about its security managment (im xp fan)...
anyway, what about FileAttributes Method (System::IO::File) ?

Maybe this will solve it once and for all ? (pity i can't test it :)

The question is if i assign attributes manualy to my files(arent they read/write available by default?) will it change anything if the folder (here program files, lets us be stubborn) is set to read only...

Last edited on
closed account (z05DSL3A)
As I understand it (more so with Vista), the files in ‘Program Files’ are not writable by non-administrative programs. UAC will even block a program run by an administrator from writing into the files.

Here are a number of possible solutions:

Probably the best would be to store your data files in a non-restricted folder, something like the users profile folder, or maybe the CommonApplicationData* folder.

Use ‘Run as administrator’ to run the program with administrative rights. This would be a good test to see if it is a rights issue.

Finally (worse) would be to change the security on the folder to give your user account write permissions.

HTH

---------------------------------
*CommonApplicationData: A system special folders that serves as a common repository for application-specific data that is used by all users. Use Environment::GetFolderPath () to obtain the path.

Console::WriteLine( "GetFolderPath: {0}", Environment::GetFolderPath( Environment::SpecialFolder:: CommonApplicationData ) );
Last edited on
Topic archived. No new replies allowed.