Click here to Skip to main content
15,881,812 members
Articles / Web Development / ASP.NET

Updating IIS Web Server Files Currently Being Downloaded

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
5 Apr 2010CPOL8 min read 35K   267   11  
Introducing a method to replace locked binary files on a web server that are currently being downloaded.
//css_pre wsdl(https://www.zeta-test.com/MyUploadService.asmx, MyUploadService);
//css_imp MyUploadService;

using System;
using System.Xml;
using System.Diagnostics;
using System.IO; 
using System.Collections.Generic; 
using CSScriptLibrary;
using System.Threading;
using System.Net;
using System.Web;

public class FillApplicationFolder
{
	#region Processing.
	// ----------------------------------------------------------------------

    public static void Main(string[] args)
    {
		Console.WriteLine();
		Console.WriteLine( "######################################################" );
		Console.WriteLine();
        Console.WriteLine( "Started script '{0}' at {1}.", scriptFileName, DateTime.Now);
		var startDate = DateTime.Now;
		Console.WriteLine( "-------------------------------------------------------");
		Console.WriteLine();
		Console.WriteLine();
		Console.WriteLine();

		try
		{
			processTasks();
		}
		catch ( Exception x )
		{
			Console.WriteLine( "+++++++++++++++++++++++++++++++++++");
			Console.WriteLine( "Error occurred:" );
			Console.WriteLine();
			Console.WriteLine( x.Message );
			Console.WriteLine();
			Console.WriteLine( x.StackTrace );
			Console.WriteLine( "+++++++++++++++++++++++++++++++++++");

			throw;
		}
		finally
		{
			Console.WriteLine();
			Console.WriteLine();
			Console.WriteLine();
			Console.WriteLine( "-------------------------------------------------------");
			var delta = DateTime.Now - startDate;
			Console.WriteLine( "Finished script '{0}' at {1}, started {2}.", scriptFileName, DateTime.Now, formateTimeSpan(delta));
			Console.WriteLine( "######################################################" );
			Console.WriteLine();
	    }
    }

	private static void processTasks()
    {
		Console.WriteLine();
		Console.WriteLine();
		Console.WriteLine( "########################################################################");
		Console.WriteLine( "## UPLOADING SETUPS.");
		Console.WriteLine( "########################################################################");
		Console.WriteLine();

		transferFiles(
			@"${ScriptFolderPath}\..\Master\zetatest-setup-core.exe",
			@"${ScriptFolderPath}\..\Master\zetatest-deployment-setup.exe",
			@"${ScriptFolderPath}\..\Master\zetatest-setup.msi",
			@"${ScriptFolderPath}\..\Master\zetatest-setup-de.exe",
			@"${ScriptFolderPath}\..\Master\zetatest-setup-en.exe"); 

		Console.WriteLine( "========================================================================");
    }

	// ----------------------------------------------------------------------
	#endregion

	#region Low level helper.
	// ----------------------------------------------------------------------

	private static void transferFiles(
		params string[] filePaths )
	{
		var index = 0;

		foreach (var filePath in filePaths )
		{
			Console.WriteLine( "Transfering file {0} of {1}.", index+1, filePaths.Length );
			transferFile( filePath );

			index++;
		}
	}

	private static void transferFile(
		string filePath )
	{
		filePath = replacePlaceholders(filePath);

		Console.WriteLine("About to execute transfer file '{0}' to server.", filePath);

		var bin = readFileBinary( filePath );

		var ws = new MyUploadService();
		ws.Timeout = 6000000;

		// http://blogs.msdn.com/joncole/archive/2005/09/08/462659.aspx
		ServicePointManager.Expect100Continue = false;

		Console.WriteLine("Using web service at URL '{0}'.", ws.Url);

		Console.WriteLine("Transfering {0:#,#} bytes to server.", bin.Length);

		ws.TransferSetupFile(
			@"17a513cb-603e-4e31-a5e7-e002b4e181c4", 
			Path.GetFileName( filePath ),
			bin );

		Console.WriteLine();
	}

	private static byte[] readFileBinary(
		string filePath )
	{
		filePath = replacePlaceholders(filePath);

		Console.WriteLine("Reading binary content from file '{0}'.", filePath);

		using ( var fs = new FileStream( filePath, FileMode.Open ) )
		using ( var br = new BinaryReader( fs ) )
		{
			var result = br.ReadBytes( (int)fs.Length );

			Console.WriteLine("Finished reading binary content from file '{0}'.", filePath);

			return result;
	    }
    }

	private static string replacePlaceholders(
		string text )
    {
		text = text.Replace( "${ScriptFolderPath}", scriptFolderPath );
		return text;
    }

	private static string scriptFolderPath
    {
		get
		{
			return Path.GetDirectoryName( csscript.CSSEnvironment.ScriptFile ).TrimEnd('\\');
		}
    }

	private static string scriptFileName
    {
		get
		{
			return Path.GetFileName( csscript.CSSEnvironment.ScriptFile );
		}
    }

	// See http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time.
	private static string formateTimeSpan(
		TimeSpan ts)
    {
		const int SECOND = 1;
		const int MINUTE = 60 * SECOND;
		const int HOUR = 60 * MINUTE;
		const int DAY = 24 * HOUR;
		const int MONTH = 30 * DAY;

		double delta = ts.TotalSeconds;

		if (delta < 1 * MINUTE)
		{
		  return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
		}
		if (delta < 2 * MINUTE)
		{
		  return "a minute ago";
		}
		if (delta < 45 * MINUTE)
		{
		  return ts.Minutes + " minutes ago";
		}
		if (delta < 90 * MINUTE)
		{
		  return "an hour ago";
		}
		if (delta < 24 * HOUR)
		{
		  return ts.Hours + " hours ago";
		}
		if (delta < 48 * HOUR)
		{
		  return "yesterday";
		}
		if (delta < 30 * DAY)
		{
		  return ts.Days + " days ago";
		}
		if (delta < 12 * MONTH)
		{
		  int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
		  return months <= 1 ? "one month ago" : months + " months ago";
		}
		else
		{
		  int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
		  return years <= 1 ? "one year ago" : years + " years ago";
		}		
    }

	// ----------------------------------------------------------------------
	#endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions