2013-05-10 59 views
0

我正在創建一個自定義構建活動,可以從根本上增加版本號。無法從文件中刪除ReadOnly屬性

我按照這個教程:

http://www.richard-banks.org/2010/07/how-to-versioning-builds-with-tfs-2010.html

本教程可能有點過時了,看到我使用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只讀刪除:\構建\ 1 ...

然而接下來的消息我得到的是:

Is ReadOnly = True

這在後面的過程中就造成一個問題,很明顯,當我嘗試使用類似WriteAllText的文件,我得到一個UnauthorizedException ...

我需要做什麼改變嗎?

感謝,

盧克

回答

2

我認爲你需要改變:

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

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

這樣做後也叫file.refresh()