1

I wanna write a C++ code to delete a file from system32 (hosts for example , a font ...) , how ? I use remove() and DeleteFile()

DeleteFile("C:/Windows/System32/drivers/etc/hosts") != 0)

remove( "C:/Windows/System32/drivers/etc/hosts" ) != 0 )

but these functions does not do that

MH SY
  • 19
  • 1
  • 2
  • 2
    What exactly doesn't work? Is your code not compiling? Does the function return an error code? Does the call succeed but the file isn't deleted? People can't really help you much if you don't fully explain your problem. – Miles Budnek Oct 12 '18 at 01:11
  • I don't mean this harshly, but you need to learn C++'s syntax (or at least C's) before trying to write anything non-trivial with it. – Acorn Oct 12 '18 at 01:12
  • 2
    are you running as an elevated administrator? – Daniel A. White Oct 12 '18 at 01:13
  • 2
    its not a good idea to delete files in there. – Daniel A. White Oct 12 '18 at 01:14
  • 1
    What does `GetLastError` tell you went wrong? – Carey Gregory Oct 12 '18 at 01:15
  • 1
    First, you need elevated privileges to write or delete from the Windows folders. Second, Windows won't allow you to delete or replace most files within those folders. Google *UAC* and *elevated privileges Windows*. Third, think very hard about why you would need to muck around in system folders that belong to the operating system and not you. – Ken White Oct 12 '18 at 01:29
  • 1
    To add to Ken White's comment, if your program has the slightest of bugs, where the file to delete is a variable and you make a mistake, you could wind up destroying your OS. Hopefully you are testing this program on a virtual machine. – PaulMcKenzie Oct 12 '18 at 01:30
  • @MilesBudnek Everything works fine and the call succeed but the file isn't deleted – MH SY Oct 12 '18 at 22:13
  • @DanielA.White Yes , I'm running as an elevated administrator – MH SY Oct 12 '18 at 22:15
  • @MHSY Please [edit] your question with a [mcve] that demonstrates that. – Miles Budnek Oct 12 '18 at 22:18

1 Answers1

0

What you need to do is run your program as Administrator, then it will have the access to delete files in this path.

Here is a tip how to do it directly from Visual Studio. If that works, you just need to set the properties of the project to require Elevation as shown in the screenshot bellow. enter image description here

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56