I had a need to download a file in MSBuild and didn’t want to assume any tools on the server. I coded together a simple task which lets you specify the URL to download, and optionally a file name or/and a output path. The goal was to keep it as small as possible, thus the bad formatting and non existent error checking.
<DownloadFile Url=”http://ftp.iinet.net.au/welcome.txt” />
<DownloadFile Url=”http://ftp.iinet.net.au/welcome.txt” OutputFolder=”D:\” File=”awesome.txt” />
https://gist.github.com/Serivy/cbf2bc3eedeecedcb5cffc2fe3e4206d
<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="DownloadFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"> <ParameterGroup> <Url ParameterType="System.String" Required="true" /> <File ParameterType="System.String" Required="false" /> <OutputFolder ParameterType="System.String" Required="false" /> </ParameterGroup> <Task> <Using Namespace="System.Web"/> <Code Type="Fragment" Language="cs"><![CDATA[ using (var client = new System.Net.WebClient()) { client.DownloadFile(Url, (OutputFolder != null ? OutputFolder + "/" : "") + (File ?? System.IO.Path.GetFileName(new Uri(Url).LocalPath))); } ]]></Code> </Task> </UsingTask> </Project> |