using System; using System.IO; class Program { static void Main() { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "yourfile.txt"); try { // 更改檔案屬性為可寫 File.SetAttributes(filePath, FileAttributes.Normal); // 刪除檔案 File.Delete(filePath); Console.WriteLine("檔案已成功刪除。"); } catch (UnauthorizedAccessException ex) { Console.WriteLine("權限問題: " + ex.Message); } catch (Exception ex) { Console.WriteLine("發生錯誤: " + ex.Message); } } }
using System; using System.IO; using System.Security.Principal; class Program { static void Main() { if (!IsRunningAsAdministrator()) { Console.WriteLine("請以管理員身份運行此應用程式。"); return; } string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "yourfile.txt"); try { File.Delete(filePath); Console.WriteLine("檔案已成功刪除。"); } catch (UnauthorizedAccessException ex) { Console.WriteLine("權限問題: " + ex.Message); } catch (Exception ex) { Console.WriteLine("發生錯誤: " + ex.Message); } } static bool IsRunningAsAdministrator() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } }