不能从文件中删除ReadOnly属性

本文关键字:ReadOnly 属性 删除 文件 不能 | 更新日期: 2023-09-27 18:12:56

我正在创建一个自定义构建活动,从根本上增加版本号。

我遵循这个教程:

http://www.richard - banks.org/2010/07/how -与- tfs版本-构建html

2010. -

这个教程可能有点过时了,因为我使用的是VS/TFS 2012,但是对于这个领域来说,我所掌握的教程内容是无关紧要的。

//Loop through files in the workspace
foreach (var file in Directory.GetFiles(folder.LocalItem, fileMask, SearchOption.AllDirectories))
{
  FileAttributes attr = File.GetAttributes(file);
  //Set the read only attribute, if we want to
  if (readOnlyFlagValue)
  {
    File.SetAttributes(file, attr | FileAttributes.ReadOnly)           
    context.TrackBuildMessage(string.Format("Set ReadOnly on {0}", file));
  }
  //Remove the readonly attribute, if we want to
  else
  {
    File.SetAttributes(file, attr | ~FileAttributes.ReadOnly);
    context.TrackBuildMessage(string.Format("Removed ReadOnly from {0}", file));
    context.TrackBuildMessage(string.Format("Is ReadOnly = {0}", File.GetAttributes(file).HasFlag(FileAttributes.ReadOnly)));
  }
}

在我的日志文件,我得到:

从C:'Builds'1…

然而,我得到的下一条消息是:

Is ReadOnly = True

这会在稍后的过程中引起问题,显然,当我尝试使用WriteAllText之类的东西到文件时,我得到UnauthorizedException

我需要改变什么?

谢谢,《路加福音》

不能从文件中删除ReadOnly属性

我认为你需要改变:

File.SetAttributes(file, attr | ~FileAttributes.ReadOnly);

File.SetAttributes(file, attr & ~FileAttributes.ReadOnly);

同时调用file.refresh()