I’ve been looking into ways to temporarily stop trying to access a network share for a while if I can’t access it in an MSBuild project. The thing is – sometimes trying to access a network share takes a really long time and if your target is executed many times by various projects – it could really slow things down and if accessing that share is only desired, but not required – it would be bad to slow things down so badly. One way I thought was interesting was to set a file or registry value as a flag based on the date, so that I will stop trying for the day, but retry the next day. Here’s how I’ve done it:
File flag
<PropertyGroup> <TodaysDate>$([System.DateTime]::Now.ToString("yyyyMMdd"))</TodaysDate> <ShareErrorFlagFile>ShareAccessError_$(TodaysDate).flag</ShareErrorFlagFile> <SharePath Condition=" '$(SharePath)' == '' And !Exists($(ShareErrorFlagFile)) And Exists('\\machine\folder')">\\machine\folder</SharePath> <SharePath Condition=" '$(SharePath)' == '' And !Exists($(ShareErrorFlagFile)) And Exists('\\machine2\folder')">\\machine\folder</SharePath> </PropertyGroup> <Target Name="test"> <CallTarget Condition="'$(SharePath)' == ''" Targets="HandleShareAccessError" /> <CallTarget Condition="'$(SharePath)' != ''" Targets="DoSomething" /> </Target> <Target Name="HandleShareAccessError"> <Message Text="Could not reach SharePath" Importance="high" /> <Exec Command="echo Failed to access build tracker install share today. Delete this file to try again. $(ShareErrorFlagFile)" /> </Target>
Registry value
<PropertyGroup> <TodaysDate>$([System.DateTime]::Now.ToString("yyyyMMdd"))</TodaysDate> <SharePath Condition=" '$(SharePath)' == '' And '$(registry:HKEY_CURRENT_USER\SOFTWARE\Contoso\MyApp@DisabledForDay)' != '$(TodaysDate)' And Exists('\\machine\folder')">\\machine\folder</SharePath> <SharePath Condition=" '$(SharePath)' == '' And '$(registry:HKEY_CURRENT_USER\SOFTWARE\Contoso\MyApp@DisabledForDay)' != '$(TodaysDate)' And Exists('\\machine2\folder')">\\machine\folder</SharePath> </PropertyGroup> <Target Name="test"> <CallTarget Condition="'$(SharePath)' == ''" Targets="HandleShareAccessError" /> <CallTarget Condition="'$(SharePath)' != ''" Targets="DoSomething" /> </Target> <Target Name="HandleShareAccessError"> <Message Text="Could not reach SharePath" Importance="high" /> <Exec Command="reg add HKCU\Software\Contoso\MyApp /v DisabledForDay /t REG_SZ /d $(TodaysDate) /f" /> </Target>
The registry key solution is slightly cleaner – it doesn’t generate date-stamped files in every place you run it, but the file-based one is more discoverable and so when you clean the directory where you run it – it will try to access the share again.