/// <summary> /// Loads the contents of a physical folder into structured storage /// </summary> /// <param name="path">The current physical path to load</param> /// <param name="container">The StorageContainer to be loaded</param> void ProcessStorageFolder(string path, StorageContainer container, WorkerProgressHandler progress) { byte[] buffer = new byte[8192]; // Local file buffer array int read; // Bytes read from the file Invoke(progress, new object[] { "Processing folder " + path }); // Add all of the files into the StorageContainer by creating new // StorageObjects for each of them and copying the data 8KB at a time foreach (string filename in Directory.GetFiles(path)) { StorageObject obj = container.Objects.Add(Path.GetFileName(filename)); using (FileStream reader = File.OpenRead(filename)) { using (StorageObjectStream writer = obj.GetWriter()) { do { read = reader.Read(buffer, 0, 8192); if (read > 0) { writer.Write(buffer, 0, read); } } while (read > 0); } } } // Now recursively create all of the subdirectories and process them foreach (string folder in Directory.GetDirectories(path)) { StorageContainer cont = container.Containers.Add(Path.GetFileName(folder)); ProcessStorageFolder(folder, cont, progress); } }
/// <summary> /// Worker thread that actually performs the file generation /// </summary> void WorkerThread() { string destFile = m_destinationFile.Text; // Set up the necessary delegates we need to invoke methods on the main thread WorkerExceptionHandler onException = new WorkerExceptionHandler(WorkerException); WorkerFinishedHandler onFinished = new WorkerFinishedHandler(WorkerFinished); WorkerProgressHandler onProgress = new WorkerProgressHandler(WorkerProgress); try { // Attempt to create/replace the main structured storage file. Notice the // completely meaningless pause for dramatic effect Invoke(onProgress, new object[] { "Creating the Virtual File System structured storage file" }); Thread.Sleep(500); using (StructuredStorage storage = StructuredStorage.Open(destFile, StorageOpenMode.Create, StorageAccessMode.Exclusive)) { // If the user did not elect to filter out non-virtualizable ASP.NET stuff, // we can just rip and roar through this without any additional work. Otherwise // use the special case handler for exclusing root items from the VFS if (!m_excludeCommon.Checked) { ProcessStorageFolder(m_sourceFolder.Text, storage, onProgress); } else { ProcessRootStorageFolderSpecial(m_sourceFolder.Text, storage, onProgress); } } Invoke(onProgress, new object[] { "The Virtual File System was generated successfully" }); Invoke(onFinished); } catch (ThreadAbortException) { // If the thread is aborting, that means the form was closed while the // process was still active. Just nuke the file and exit the thread if (File.Exists(destFile)) { File.Delete(destFile); } } catch (Exception exception) { if (File.Exists(destFile)) { File.Delete(destFile); } // Standard exceptions get pumped back into the main thread to be // reported. Also invoke the finish handler since I'm not really // spending a lot of time making this a top-notch application :) Invoke(onException, new object[] { exception }); Invoke(onProgress, new object[] { "The Virtual File System could not be generated" }); Invoke(onFinished); } }
/// <summary> /// Special case version of ProcessStorage folder to deal with the root ASP.NET /// folder exclusions like web.config, \bin, \app_code, etc. /// </summary> /// <param name="path">The current physical path to load</param> /// <param name="container">The StorageContainer to be loaded</param> void ProcessRootStorageFolderSpecial(string path, StorageContainer container, WorkerProgressHandler progress) { byte[] buffer = new byte[8192]; // Local file buffer array int read; // Bytes read from the file string nameOnly; // File name only portion of the path Invoke(progress, new object[] { "Processing folder " + path }); // Add all of the files into the StorageContainer by creating new // StorageObjects for each of them and copying the data 8KB at a time foreach (string filename in Directory.GetFiles(path)) { nameOnly = Path.GetFileName(filename); // Get the file name // SPECIAL CASE FILES if (nameOnly.StartsWith("global.asax", StringComparison.OrdinalIgnoreCase)) { continue; } if (nameOnly.Equals("web.config", StringComparison.OrdinalIgnoreCase)) { continue; } if ((Path.GetExtension(nameOnly) == ".xml") && (IsFileSiteMapXml(filename))) { continue; } StorageObject obj = container.Objects.Add(Path.GetFileName(nameOnly)); using (FileStream reader = File.OpenRead(filename)) { using (StorageObjectStream writer = obj.GetWriter()) { do { read = reader.Read(buffer, 0, 8192); if (read > 0) { writer.Write(buffer, 0, read); } } while (read > 0); } } } // Now recursively create all of the subdirectories and process them foreach (string folder in Directory.GetDirectories(path)) { nameOnly = Path.GetFileName(folder); // Get the folder name // SPECIAL CASE FOLDERS if (nameOnly.Equals("bin", StringComparison.OrdinalIgnoreCase)) { continue; } if (nameOnly.Equals("app_code", StringComparison.OrdinalIgnoreCase)) { continue; } if (nameOnly.Equals("app_data", StringComparison.OrdinalIgnoreCase)) { continue; } if (nameOnly.Equals("app_globalresources", StringComparison.OrdinalIgnoreCase)) { continue; } if (nameOnly.Equals("app_localresources", StringComparison.OrdinalIgnoreCase)) { continue; } StorageContainer cont = container.Containers.Add(Path.GetFileName(nameOnly)); ProcessStorageFolder(folder, cont, progress); } }
/// <summary> /// Worker thread that actually performs the file generation /// </summary> void WorkerThread() { string destFile = m_destinationFile.Text; // Set up the necessary delegates we need to invoke methods on the main thread WorkerExceptionHandler onException = new WorkerExceptionHandler(WorkerException); WorkerFinishedHandler onFinished = new WorkerFinishedHandler(WorkerFinished); WorkerProgressHandler onProgress = new WorkerProgressHandler(WorkerProgress); try { // Attempt to create/replace the main structured storage file. Notice the // completely meaningless pause for dramatic effect Invoke(onProgress, new object[] { "Creating the Virtual File System structured storage file" }); Thread.Sleep(500); using (StructuredStorage storage = StructuredStorage.Open(destFile, StorageOpenMode.Create, StorageAccessMode.Exclusive)) { // If the user did not elect to filter out non-virtualizable ASP.NET stuff, // we can just rip and roar through this without any additional work. Otherwise // use the special case handler for exclusing root items from the VFS if (!m_excludeCommon.Checked) ProcessStorageFolder(m_sourceFolder.Text, storage, onProgress); else ProcessRootStorageFolderSpecial(m_sourceFolder.Text, storage, onProgress); } Invoke(onProgress, new object[] { "The Virtual File System was generated successfully" }); Invoke(onFinished); } catch (ThreadAbortException) { // If the thread is aborting, that means the form was closed while the // process was still active. Just nuke the file and exit the thread if (File.Exists(destFile)) File.Delete(destFile); } catch (Exception exception) { if (File.Exists(destFile)) File.Delete(destFile); // Standard exceptions get pumped back into the main thread to be // reported. Also invoke the finish handler since I'm not really // spending a lot of time making this a top-notch application :) Invoke(onException, new object[] { exception }); Invoke(onProgress, new object[] { "The Virtual File System could not be generated" }); Invoke(onFinished); } }
/// <summary> /// Loads the contents of a physical folder into structured storage /// </summary> /// <param name="path">The current physical path to load</param> /// <param name="container">The StorageContainer to be loaded</param> void ProcessStorageFolder(string path, StorageContainer container, WorkerProgressHandler progress) { byte[] buffer = new byte[8192]; // Local file buffer array int read; // Bytes read from the file Invoke(progress, new object[] { "Processing folder " + path }); // Add all of the files into the StorageContainer by creating new // StorageObjects for each of them and copying the data 8KB at a time foreach (string filename in Directory.GetFiles(path)) { StorageObject obj = container.Objects.Add(Path.GetFileName(filename)); using (FileStream reader = File.OpenRead(filename)) { using (StorageObjectStream writer = obj.GetWriter()) { do { read = reader.Read(buffer, 0, 8192); if (read > 0) writer.Write(buffer, 0, read); } while (read > 0); } } } // Now recursively create all of the subdirectories and process them foreach (string folder in Directory.GetDirectories(path)) { StorageContainer cont = container.Containers.Add(Path.GetFileName(folder)); ProcessStorageFolder(folder, cont, progress); } }
/// <summary> /// Special case version of ProcessStorage folder to deal with the root ASP.NET /// folder exclusions like web.config, \bin, \app_code, etc. /// </summary> /// <param name="path">The current physical path to load</param> /// <param name="container">The StorageContainer to be loaded</param> void ProcessRootStorageFolderSpecial(string path, StorageContainer container, WorkerProgressHandler progress) { byte[] buffer = new byte[8192]; // Local file buffer array int read; // Bytes read from the file string nameOnly; // File name only portion of the path Invoke(progress, new object[] { "Processing folder " + path }); // Add all of the files into the StorageContainer by creating new // StorageObjects for each of them and copying the data 8KB at a time foreach (string filename in Directory.GetFiles(path)) { nameOnly = Path.GetFileName(filename); // Get the file name // SPECIAL CASE FILES if (nameOnly.StartsWith("global.asax", StringComparison.OrdinalIgnoreCase)) continue; if (nameOnly.Equals("web.config", StringComparison.OrdinalIgnoreCase)) continue; if ((Path.GetExtension(nameOnly) == ".xml") && (IsFileSiteMapXml(filename))) continue; StorageObject obj = container.Objects.Add(Path.GetFileName(nameOnly)); using (FileStream reader = File.OpenRead(filename)) { using (StorageObjectStream writer = obj.GetWriter()) { do { read = reader.Read(buffer, 0, 8192); if (read > 0) writer.Write(buffer, 0, read); } while (read > 0); } } } // Now recursively create all of the subdirectories and process them foreach (string folder in Directory.GetDirectories(path)) { nameOnly = Path.GetFileName(folder); // Get the folder name // SPECIAL CASE FOLDERS if (nameOnly.Equals("bin", StringComparison.OrdinalIgnoreCase)) continue; if (nameOnly.Equals("app_code", StringComparison.OrdinalIgnoreCase)) continue; if (nameOnly.Equals("app_data", StringComparison.OrdinalIgnoreCase)) continue; if (nameOnly.Equals("app_globalresources", StringComparison.OrdinalIgnoreCase)) continue; if (nameOnly.Equals("app_localresources", StringComparison.OrdinalIgnoreCase)) continue; StorageContainer cont = container.Containers.Add(Path.GetFileName(nameOnly)); ProcessStorageFolder(folder, cont, progress); } }