Dumping files from the episerver filesystem, developer style

by erik.nilsson 11. March 2010 14:26

Since webdav is nasty and never work for me, and the boys and gals over at episerver need some lessons in usability (who thought of the idea to only be able to select files one at a time with the export tool?). I made a small code snippet to dump the contents of a entire directory, like so:

public sealed class DumpaRump
	{
		public static void DumpVirtualPath(string providerName, string outputRoot)
		{
			VirtualPathUnifiedProvider provider = (VirtualPathUnifiedProvider)VirtualPathHandler.GetProvider(providerName);
			var dir = provider.GetDirectory(provider.VirtualPathRoot);
			DumpVirtualDirectory(dir, outputRoot);
		}

		private static void DumpVirtualDirectory(VirtualDirectory directory, string outputDirectory)
		{
			foreach (var subDirectory in directory.Directories)
			{
				var virtualSubDirectory = (VirtualDirectory)subDirectory;
				DumpVirtualDirectory(virtualSubDirectory, string.Concat(outputDirectory, virtualSubDirectory.Name, "\\"));
			}

			if (!Directory.Exists(outputDirectory))
			{
				Directory.CreateDirectory(outputDirectory);
			}

			foreach (var file in directory.Files)
			{
				var virtualFile = (VirtualFile)file;

				try
				{
					using (var virtualStream = virtualFile.Open())
					{
						using (var fileStream = File.Create(string.Concat(outputDirectory, virtualFile.Name)))
						{
							byte[] buffer = new byte[8 * 1024];
							int len;
							while ((len = virtualStream.Read(buffer, 0, buffer.Length)) > 0)
							{
								fileStream.Write(buffer, 0, len);
							}
						}
					}
				}
				catch
				{
					// Something wrong with this file, some fancy errorhandling should probably be made here
				}
			}
		}
	}
And you can call it for eample like this:
	DumpaRump.DumpVirtualPath("SiteGlobalFiles", "C:\\Temp\\GlobalDump\\");

 

Enjoy.

Tags:

blog comments powered by Disqus

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.