Click here to Skip to main content
15,892,253 members
Articles / Programming Languages / Visual Basic

QVFS - A FAT32 Based Virtual File System Powered by VB.NET 2005

Rate me:
Please Sign up or sign in to vote.
4.68/5 (33 votes)
8 Jun 2009CPOL5 min read 160.6K   1.7K   100  
A virtual file system created using VB.NET, an easy way to store multiple folders and files in two real physical files.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using QVFS;
using System.Net;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Tester
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnCreate;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnSize;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnCreate = new System.Windows.Forms.Button();
			this.btnOpen = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnSize = new System.Windows.Forms.ColumnHeader();
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.btnAdd = new System.Windows.Forms.Button();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnCreate
			// 
			this.btnCreate.Location = new System.Drawing.Point(16, 40);
			this.btnCreate.Name = "btnCreate";
			this.btnCreate.Size = new System.Drawing.Size(75, 23);
			this.btnCreate.TabIndex = 0;
			this.btnCreate.Text = "Create";
			this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(97, 40);
			this.btnOpen.Name = "btnOpen";
			this.btnOpen.Size = new System.Drawing.Size(75, 24);
			this.btnOpen.TabIndex = 1;
			this.btnOpen.Text = "Open";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnSize});
			this.listView1.Location = new System.Drawing.Point(184, 80);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(464, 272);
			this.listView1.TabIndex = 2;
			this.listView1.UseCompatibleStateImageBehavior = false;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "FileName";
			this.columnHeader1.Width = 100;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "ClusterStartIndex";
			this.columnHeader2.Width = 100;
			// 
			// columnSize
			// 
			this.columnSize.Text = "Size";
			this.columnSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.columnSize.Width = 100;
			// 
			// treeView1
			// 
			this.treeView1.HideSelection = false;
			this.treeView1.Location = new System.Drawing.Point(8, 80);
			this.treeView1.Name = "treeView1";
			this.treeView1.Size = new System.Drawing.Size(160, 272);
			this.treeView1.TabIndex = 3;
			this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(330, 40);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(75, 23);
			this.btnAdd.TabIndex = 4;
			this.btnAdd.Text = "Add";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.Location = new System.Drawing.Point(418, 40);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(75, 23);
			this.btnDelete.TabIndex = 5;
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(538, 40);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(110, 23);
			this.btnSave.TabIndex = 6;
			this.btnSave.Text = "Save to Hard Disk";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(184, 40);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 7;
			this.button1.Text = "Refresh";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(664, 369);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.treeView1);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.btnOpen);
			this.Controls.Add(this.btnCreate);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new Form1());
		}

		private void btnCreate_Click(object sender, System.EventArgs e)
		{
			if (MessageBox.Show("Create ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
			{
				QVFS.QVirtualFileSystemManager.CreateQVFSFile("C:\\test.qfi", true);
				MessageBox.Show("Virtual File Created, 'C:\\test.qfi'");
			}
		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			if (File.Exists("C:\\test.qfi"))
			{
				QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
				vfm.OpenQVFSFile("C:\\test.qfi");
				TreeNode node = new TreeNode("\\");
				this.treeView1.Nodes.Add(node);

				GetDirs(vfm, vfm.GetVDirectoryList("\\"), node);
				vfm.CloseQVFSFile();
			}
			else
			{
				MessageBox.Show("Demo file not found, click 'Create' first.");
			}
		}

		private void GetDirs(QVFS.QVirtualFileSystemManager vfm, FileEntryCollection vfiles, TreeNode pNode)
		{
			foreach (FileEntry vfile in vfiles)
			{
				TreeNode node = pNode.Nodes.Add(vfile.Name);
				GetDirs(vfm, vfm.GetVDirectoryList(pNode.FullPath + "\\" + vfile.Name), node);
			}
		}

		private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			this.listView1.Items.Clear();

			QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
			vfm.OpenQVFSFile("C:\\test.qfi");
			FileEntryCollection vfiles = vfm.GetVFileList(e.Node.FullPath, false);
			foreach (FileEntry vfile in vfiles)
			{
				ListViewItem item = new ListViewItem(vfile.Name);
				item.SubItems.Add(vfile.ClusterStartIndex.ToString());
				item.SubItems.Add(vfile.Size.ToString("#,###"));

				this.listView1.Items.Add(item);
			}
			vfm.CloseQVFSFile();
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{


			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
				vfm.OpenQVFSFile("C:\\test.qfi");
				vfm.AddNewVFile(this.openFileDialog1.FileName, this.treeView1.SelectedNode.FullPath + "\\" + System.IO.Path.GetFileName(this.openFileDialog1.FileName), true);

				//Refresh list
				FileEntryCollection vfiles = vfm.GetVFileList(treeView1.SelectedNode.FullPath, false);
				foreach (FileEntry vfile in vfiles)
				{
					ListViewItem item = new ListViewItem(vfile.Name);
					item.SubItems.Add(vfile.ClusterStartIndex.ToString());
					item.SubItems.Add(vfile.Size.ToString("#,###"));

					this.listView1.Items.Add(item);
				}

				vfm.CloseQVFSFile();

			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{

			if (this.listView1.SelectedItems.Count > 0)
			{
				if (MessageBox.Show("Delete?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
					vfm.OpenQVFSFile("C:\\test.qfi");
					if (this.treeView1.SelectedNode.FullPath.EndsWith(@"\"))
					{
						vfm.DeleteVFile(this.treeView1.SelectedNode.FullPath + this.listView1.SelectedItems[0].Text, true);

					}
					else
					{
						vfm.DeleteVFile(this.treeView1.SelectedNode.FullPath + @"\" + this.listView1.SelectedItems[0].Text, true);

					}

					//Refresh list
					FileEntryCollection vfiles = vfm.GetVFileList(treeView1.SelectedNode.FullPath, false);
					foreach (FileEntry vfile in vfiles)
					{
						ListViewItem item = new ListViewItem(vfile.Name);
						item.SubItems.Add(vfile.ClusterStartIndex.ToString());
						item.SubItems.Add(vfile.Size.ToString("#,###"));

						this.listView1.Items.Add(item);
					}

					vfm.CloseQVFSFile();
				}
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if (this.listView1.SelectedItems.Count > 0)
			{
				if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
				{
					QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
					vfm.OpenQVFSFile("C:\\test.qfi");
					vfm.SaveVFileToDisk(this.treeView1.SelectedNode.FullPath + "\\" + this.listView1.SelectedItems[0].Text, this.saveFileDialog1.FileName);
					vfm.CloseQVFSFile();
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.listView1.Items.Clear();

			QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
			vfm.OpenQVFSFile("C:\\test.qfi");
			FileEntryCollection vfiles = vfm.GetVFileList(treeView1.SelectedNode.FullPath, false);
			foreach (FileEntry vfile in vfiles)
			{
				ListViewItem item = new ListViewItem(vfile.Name);
				item.SubItems.Add(vfile.ClusterStartIndex.ToString());
				item.SubItems.Add(vfile.Size.ToString("#,###"));

				this.listView1.Items.Add(item);
			}
			vfm.CloseQVFSFile();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			MessageBox.Show("This is just a DEMO program for the QVFS library. Click 'Create' first to create the DEMO virtual disk file  which will be located at 'C:\\test.*'", "Introduce", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}


	}


}

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
Architect
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions