Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Windows C#4.0 Win7 .NET4
How to create a file inside Program Files or Windows folder of C drive when I am trying to create a file in windows form application it will through an exception

"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\Program Files\test.xml' is denied."

Is there any way to create file inside this 2 folder
please suggest me how would I do.
Posted 7-Sep-12 2:36am
Edited 7-Sep-12 2:44am
Wes Aday at 7-Sep-12 7:40am
run as administrator
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to run your application with Administrator Privilege.
Change following settings into your application Manifest.
<requestedexecutionlevel level="requireAdministrator" uiaccess="false" />
Prafulla Sahu at 7-Sep-12 7:45am
Thanks Malli for your reply but sorry Malli I need for windows application
Malli_S at 7-Sep-12 7:54am
That IS for windows application. If you don't have manifest file you can add one and update the above specified setting.
Eddy Vluggen at 7-Sep-12 8:03am
Although your solution works, I have to disagree; it's a silly request, and we don't write in those locations. At all. Ever.
The only reason to request something like this, is when someone wants to HIDE his app from the user - your typical trojan-writer.
Malli_S at 7-Sep-12 8:37am
I agree with you. :)
Prafulla Sahu at 11-Sep-12 4:33am
Thanks mali and Eddy for your reply but I want to hide my app.good guess Eddy and malli
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should store your data elsewhere. It is "not done" to write in the Program Files folder, nor the Windows-folder. These are special system-folders, and your app should not touch them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Mobile
Web02 | 2.8.150326.1 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100