0

I'm going to copy a file to this path(C:\Windows\Win32) in windows 7 using C# programming(the "Win32" Folder created by me) but I can't.Because the operation system don't allow me.so I need to full access . I write this code for solving the problem , but I see this error in the program running : " Attempted to perform an unauthorized operation." . please help me !!! .thanks

using System.IO;
using System.Security.AccessControl;
using System.Management;
using System.Management.Instrumentation;

 1. private void Sample2_Click(object sender, EventArgs e)
 2. {
 3.    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\Windows\Win32");
 4.    FileSystemAccessRule fsar = new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow);
 5.    DirectorySecurity ds = null;
 6.    if (!di.Exists)
 7.    {
 8.       System.IO.Directory.CreateDirectory(@"C:\Windows\Win32");
 9.    }
 10.    ds = di.GetAccessControl();
 11.    ds.AddAccessRule(fsar);
 12.    di.SetAccessControl(ds);
 13.    File.Copy(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", @"C:\Windows\Win32\Desert.jpg");
 14.    MessageBox.Show("successfully copy!!!!");
 15. }

1 Answers1

0

You will need to trigger UAC when starting your application.

Either embed this XML in your manifest-file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
     <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
       <security>
         <requestedPrivileges>
           <requestedExecutionLevel
             level="asInvoker"
             uiAccess="false"/>
           </requestedPrivileges>
          </security>
     </trustInfo>
   </assembly>

or you can make use of this sample code from MSDN or look here or even on SO. There is plenty of stuff.

Community
  • 1
  • 1
bash.d
  • 13,029
  • 3
  • 29
  • 42