[英]How do you delete all files from the windows Temp folder using C#? (When the processes are running)
[英]Can I delete files from “Windows” folder in Windows Server 2016 using C# (MVC or Core)?
在 windows 服務器 2016 中,由於某些原因,我在“Windows”文件夾中創建了一個文本文件,在特殊情況下,我需要從我的網站中刪除它,該網站內置於 C# MVC 中,有什么方法可以使用 ZD7EFA19FBE7D39DFDZ4(核)? 我知道這是不合邏輯的,但如果適用,我需要它。
您的解決方案的答案相當簡單,是的,確實可以刪除 Windows 文件或文件夾。 然而,這是昂貴的。 允許應用程序具有管理員權限可能會導致惡意行為。 例如 UAC 繞過其他惡意軟件。
為了使用 C# 刪除該文件編程,您需要做的就是首先以管理員權限運行您的應用程序。 這是它的代碼源:
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);
}
}
}
權限風險文檔: 管理員權限風險
File.Delete 的文檔: File.Delete(String) 方法 C#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.