C# update FileAttributes

System.UnauthorizedAccessException: 'Access to the path 'D:\C\ConsoleApp13\bin\Debug\net8.0\ConsoleApp13.deps.json' is denied.'

 

 static void Main(string[] args)
 {
     string filePath = "ConsoleApp13.deps.json";
     UpdateFileAttributeReadonlyToArchive(filePath);
     PrintLog();
 }

 static void UpdateFileAttributeReadonlyToArchive(string filePath=null)
 {            
     if(File.Exists(filePath))
     {
         FileAttributes fa = File.GetAttributes(filePath);
         if ((fa & FileAttributes.ReadOnly) != 0)
         {
             fa ^= FileAttributes.ReadOnly;
             File.SetAttributes(filePath, fa);
         }
         File.Delete(filePath);
     } 
 }

 

posted @ 2024-02-28 21:08  FredGrit  阅读(1)  评论(0编辑  收藏  举报