⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webfileloader.cs

📁 饭店类的C#程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;
using System.Xml;
using System.Collections;
using System.Globalization;

namespace Microsoft.Samples.AppUpdater
{
	//**************************************************************
	// WebFileLoader class
	// - Full of static helper methods for network & disk I/O
	//**************************************************************
	public class WebFileLoader
	{

		//**************************************************************
		// CheckForFileUpdate()	
		// - Checks to see if a newer file is on the server
		//**************************************************************
		public static bool CheckForFileUpdate(string url, string filePath)
		{
			HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
			Request.Method = "HEAD";
			Request.Credentials = CredentialCache.DefaultCredentials;

			//Set up the last modfied time header
			if (File.Exists(filePath)) 
				Request.IfModifiedSince = LastModFromDisk(filePath);

			HttpWebResponse Response;
			try 
			{
				Response = (HttpWebResponse)Request.GetResponse();
			}
			catch(WebException e) 
			{
				if (e.Response == null)
				{
					Debug.WriteLine("Error accessing Url " + url);
					throw;
				}
			
				HttpWebResponse errorResponse = (HttpWebResponse)e.Response;

				//if the file has not been modified
				if (errorResponse.StatusCode == HttpStatusCode.NotModified)
				{
					e.Response.Close();
					return false;
				}
				else 
				{
					e.Response.Close();
					Debug.WriteLine("Error accessing Url " + url);
					throw;
				}
			} 
			//This case happens if no lastmodedate was specified, but the specified
			//file does exist on the server. 
			Response.Close();
			return true;
		}

		//**************************************************************
		// UpdateFile()	
		// - Copies the newer file on the server to the client
		//**************************************************************
		public static void UpdateFile(string url, string filePath)
		{
			HttpWebResponse Response;
			
			//Retrieve the File
			HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
			Request.Headers.Add("Translate: f");
			Request.Credentials = CredentialCache.DefaultCredentials;

			//Set up the last modfied time header
			if (File.Exists(filePath)) 
				Request.IfModifiedSince = LastModFromDisk(filePath);

			try 
			{
				Response = (HttpWebResponse)Request.GetResponse();
			}
			catch(WebException e) 
			{
				if (e.Response == null) 
				{
					Debug.WriteLine("Error accessing Url " + url);
					throw;
				}

				HttpWebResponse errorResponse = (HttpWebResponse)e.Response;
				
				//if the file has not been modified
				if (errorResponse.StatusCode == HttpStatusCode.NotModified)
				{
					e.Response.Close();
					return;
				}
				else 
				{
					e.Response.Close();
					Debug.WriteLine("Error accessing Url " + url);
					throw;
				}
			}
		
			Stream respStream = null;

			try 
			{
				respStream = Response.GetResponseStream();
				CopyStreamToDisk(respStream,filePath);

				DateTime d = System.Convert.ToDateTime(Response.GetResponseHeader("Last-Modified"));
				File.SetLastWriteTime(filePath,d);
			} 
			catch (Exception)
			{
				Debug.WriteLine("APPMANAGER:  Error writing to:  " + filePath);
				throw;
			}
			finally 
			{
				if (respStream != null)
					respStream.Close();
				if (Response != null)
					Response.Close();
			}
		}

		//**************************************************************
		// CopyDirectory()	
		// Does a deep copy
		// Returns the number of files copied
		// If keys is null, validates assemblies copied have been signed
		// by one of the provided keys.
		//**************************************************************
		public static int CopyDirectory(string url, string filePath)
		{
			Resource currentResource;
			int FileCount = 0;
			string newFilePath;
			SortedList DirectoryList = WebFileLoader.GetDirectoryContents(url, false);

			foreach(Object r in DirectoryList) 
			{
				currentResource = (Resource)(((DictionaryEntry)r).Value);
	
				//If the directory doesn't exist, create it first
				if (!Directory.Exists(filePath))
					Directory.CreateDirectory(filePath);
		
				//If it's a file than copy the file.
				if (!currentResource.IsFolder)
				{
					FileCount++;
					newFilePath = filePath + currentResource.Name;
			
					//only update the file if it doesn't exist or if the one on the server
					//returned a newer last modtime than the one on disk
					if (!File.Exists(newFilePath))
						UpdateFile(currentResource.Url,newFilePath);
					else
						if (currentResource.LastModified > LastModFromDisk(newFilePath))
							UpdateFile(currentResource.Url,newFilePath);				

				}
				//If it's a folder, download the folder itself.
				else
				{
					newFilePath = filePath + currentResource.Name + "\\";
					FileCount += CopyDirectory(currentResource.Url,newFilePath);
				}	
			}
			return FileCount;
		}

		//**************************************************************
		// GetDirectoryContents()	
		// - Uses HTTP/DAV to get a list of directories
		//**************************************************************
		public static SortedList GetDirectoryContents(string url, bool deep)
		{
			//Retrieve the File
			HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
			Request.Headers.Add("Translate: f");
			Request.Credentials = CredentialCache.DefaultCredentials;

			string requestString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
				"<a:propfind xmlns:a=\"DAV:\">"+
				"<a:prop>"+
				"<a:displayname/>"+
				"<a:iscollection/>"+
				"<a:getlastmodified/>"+
				"</a:prop>"+
				"</a:propfind>";
			
			Request.Method = "PROPFIND";
			if (deep == true)
				Request.Headers.Add("Depth: infinity");
			else
				Request.Headers.Add("Depth: 1");
			Request.ContentLength = requestString.Length;
			Request.ContentType	= "text/xml";

			Stream requestStream = Request.GetRequestStream();
			requestStream.Write(Encoding.ASCII.GetBytes(requestString),0,Encoding.ASCII.GetBytes(requestString).Length);
			requestStream.Close();

			HttpWebResponse Response;
			StreamReader respStream;
			try 
			{
				Response = (HttpWebResponse)Request.GetResponse();
				respStream = new StreamReader(Response.GetResponseStream());
			}
			catch (WebException e)
			{
				Debug.WriteLine("APPMANAGER:  Error accessing Url " + url);
				throw e;
			}

			StringBuilder SB = new StringBuilder(); 
						
			char[] respChar = new char[1024];
			int BytesRead = 0;

			BytesRead = respStream.Read(respChar,0,1024);
			
			while (BytesRead>0)
			{
				SB.Append(respChar,0,BytesRead);
				BytesRead = respStream.Read(respChar,0,1024);				
			}
			respStream.Close();

			XmlDocument XmlDoc = new XmlDocument();
			XmlDoc.LoadXml(SB.ToString());
			      
			//Create an XmlNamespaceManager for resolving namespaces.
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(XmlDoc.NameTable);
			nsmgr.AddNamespace("a", "DAV:");

			XmlNodeList NameList = XmlDoc.SelectNodes("//a:prop/a:displayname",nsmgr);
			XmlNodeList isFolderList = XmlDoc.SelectNodes("//a:prop/a:iscollection",nsmgr);
			XmlNodeList LastModList = XmlDoc.SelectNodes("//a:prop/a:getlastmodified",nsmgr);
			XmlNodeList HrefList = XmlDoc.SelectNodes("//a:href",nsmgr);

			SortedList ResourceList = new SortedList();
			Resource tempResource;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -