简体   繁体   中英

Can I delete files from “Windows” folder in Windows Server 2016 using C# (MVC or Core)?

In windows server 2016, I create a text file inside "Windows" folder for some reasons, and in special cases I need to delete it from my website which is built in C# MVC, is there any way to do that using C# (MVC or Core)? I know that it is illogical but I need that if applicable.

The answer for your solution is fairly simple, yes it is indeed possible to remove a Windows file or folder. However, this is costly. Allowing an application to have Administrator permissions can lead to malicious behaviors. Such as UAC Bypassing from other malware.

All you need to do in order to delete that file programming using C# is to first off run your application with administrator rights. Here's source the code for it:

using System;
using System.IO;
public class Program {
   public static void Main() {
      String myPath = @"<DRIVE_LETTER>:\Windows\<FILENAME>"; // E.g: @"C:\Windows\MyText.txt";
      try{ // For stability purposes,
          File.Delete(myPath);
      } catch (IOException ERROR){ // If any errors occurs, it will print it out!
          Console.WriteLine(ERROR.Message);
      }
   }
} 

Documentation for Permission(s) Risks: Risks of Admin Rights

Documentation for File.Delete: File.Delete(String) Method C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM