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);
            }                 
        }

 

posted @ 2020-06-08 18:21  FredGrit  阅读(158)  评论(0编辑  收藏  举报