Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please i need help
i tried more of time and different ways to copy file through my Desktop Application by C#.Net



C#
using (FileStream fs = new FileStream(SelectedFiles, FileMode.Open, FileAccess.Read))
                       {
                           FileIOPermission fp1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, SelectedFiles);
                           FileIOPermission fp2 = new FileIOPermission(FileIOPermissionAccess.AllAccess, StaticPath);

                           System.Security.AccessControl.FileSecurity SelectedFile = new System.Security.AccessControl.FileSecurity(SelectedFiles, System.Security.AccessControl.AccessControlSections.All);
                           System.IO.File.SetAttributes(SelectedFiles, FileAttributes.Normal);
                           System.IO.File.SetAccessControl(SelectedFiles, SelectedFile);
                           System.Security.AccessControl.FileSecurity MyPath = new System.Security.AccessControl.FileSecurity(StaticPath, System.Security.AccessControl.AccessControlSections.All);
                           System.IO.File.SetAttributes(StaticPath, FileAttributes.Normal);
                           System.IO.File.SetAccessControl(StaticPath, MyPath);

                          // System.IO.Path.Combine(SelectedFiles, StaticPath);
                           System.IO.File.Copy(StaticPath, SelectedFiles, true);
                       }
Posted
Comments
PIEBALDconsult 15-Oct-14 11:39am    
Well... what _is_ the path? Which statememt throws the error? What error?
NaibedyaKar 15-Oct-14 11:42am    
I think your application is not given permission to access that path. Can you run your application as an administrator. Right-click on it and select "Run as Administrator". If still not working, let me know. Provide me the path that you are trying access. Change the path and try to access.
ZurdoDev 15-Oct-14 12:07pm    
As stated in comments and in the error, this is a permissions issue.

1 solution

If access to some file system object is denied, you cannot fix its access by some application via any manipulations inside this application. Just think about it: if UAC (http://en.wikipedia.org/wiki/User_Account_Control[^], read about it thoroughly) could be worked around, why would anyone create and use it?

You can only elevate privilege for your application run your application "As Administrator", or use elevation of your privileges to modify the access to the file system object in question. Please see: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

Alternatively, you can create an embed the application manifest, to force elevation automatically, from the very beginning: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

No, it won't bypass the UAC dialog. :-)

—SA
 
Share this answer
 
Comments
BillWoodruff 15-Oct-14 17:56pm    
+5
Sergey Alexandrovich Kryukov 15-Oct-14 18:10pm    
Thank you, Bill.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900