Change file readonly property File.SetAttribute and new FileInfo readonly property

 
  
 static void FileInfoChangeReadOnly()
        {
            string file = @"D:\C\ConsoleApp24\ConsoleApp24\bin\Debug\netcoreapp3.1\ConsoleApp23\ConsoleApp23\bin\Debug\netcoreapp3.1\original.txt";
            try
            {
                File.Delete(file);
            }
            catch
            {
                FileInfo fi = new FileInfo(file);
                fi.IsReadOnly = false;
                File.Delete(file);
            }            
        }
 
  

 

static void FileGetAttributes()
        {
            string file = @"D:\C\ConsoleApp24\ConsoleApp24\bin\Debug\netcoreapp3.1\ConsoleApp23\ConsoleApp23\bin\Debug\netcoreapp3.1\compressed.cmp";
            try
            {
                File.Delete(file);
            }
            catch
            {
                FileAttributes fas = File.GetAttributes(file);
                if ((fas & FileAttributes.ReadOnly) != 0)
                {
                    fas ^= FileAttributes.ReadOnly;
                    File.SetAttributes(file, fas);
                }
                File.Delete(file);
            }                 
        }

 

你可能感兴趣的:(Change file readonly property File.SetAttribute and new FileInfo readonly property)