Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows C#4.0 Win7 .NET4
hi
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.
 
Thanks
Prafulla
Posted 7-Sep-12 1:36am
Edited 7-Sep-12 1:44am
v2
Comments
Wes Aday at 7-Sep-12 7:40am
   
run as administrator
Rate this: bad
good
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" />
  Permalink  
Comments
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
good
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.
  Permalink  

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

  Print Answers RSS
0 CPallini 405
1 Sergey Alexandrovich Kryukov 272
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2014
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