2

I want to copy a file into a folder, where I need admin permission.

Example:

string oldPath = "C:\\Users\\Sony\\Desktop\\a.txt";
string newPath = "C:\\ProgramData\\Microsoft\\Network\\a.txt";

int main (){
     MoveFile(oldPath.c_str(), newPath.c_str());

     // now I have to send the permission, because the file won't be copied

     return 0;
}

How can I send these permission to copy a file into such folders?

Thanks guys

Søny
  • 109
  • 1
  • 7
  • 1
    Please put the definition of MoveFile function so we can see what's going on ! – Conjecture Sep 05 '17 at 19:37
  • 3
    Flagging this as too broad as the are complete books on MS-Windows Access control and file permissions. – Richard Critten Sep 05 '17 at 19:38
  • 1
    @MarwanB `MoveFile` is a Win32 API function: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx – Richard Critten Sep 05 '17 at 19:39
  • @PhyToniC - you need to [elevate your process](https://stackoverflow.com/questions/8676015/how-can-i-elevate-my-process-at-runtime-under-win-xp) so that it runs with admin rights. Or start it as Admin. Either way the user will be prompted to allow the process to run as admin (provided the user actually has admin rights). – rustyx Sep 05 '17 at 19:49
  • https://stackoverflow.com/questions/7087958/file-stat-vs-access-to-check-permissions-on-a-directory – Hariom Singh Sep 05 '17 at 19:52

2 Answers2

1

Run the .exe file as an administrator.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MartijnKor
  • 82
  • 1
  • 9
0

You have a few choices:

  1. run the app as an admin. This gives the entire app admin rights for its entire lifetime. You should strive not to do this when possible, unless your entire app really needs admin rights at all times. Otherwise, strive to run with least privileges as much as possible.

  2. gain admin access temporarily only during the file copy:

    • impersonate an admin user, do the file copy, and then revert back to the original user when finished.

    • move the file copy logic to a separate process that can be run elevated using ShellExecute/Ex("runas").

    • move the file copy logic to a COM object that can be run elevated using the COM Elevation Moniker.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770