private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (Properties.Settings.Default["vfsfile"] == null || ((String)(Properties.Settings.Default["vfsfile"])).Length == 0)
{
var r = System.Windows.Forms.MessageBox.Show("首次启动 VFS 需要在磁盘上建立一个虚拟磁盘镜像文件,镜像文件大小为 1GB。\n点击确定后请选择一个可以写入的位置来建立此文件,点击取消关闭程序。", "磁盘镜像文件未找到", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Information);
if (r == System.Windows.Forms.DialogResult.Cancel)
{
Close();
return;
}
using (var dialog = new System.Windows.Forms.SaveFileDialog())
{
dialog.Title = "保存磁盘镜像文件";
dialog.FileName = "vfs.bin";
var result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
Close();
return;
}
else
{
try
{
device = new FileAdapter(dialog.FileName, 1 << 10 << 10 << 10);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("打开镜像文件失败,可能您没有权限在该位置写入文件,或您内存不足。程序即将退出,请重新打开程序。");
Close();
return;
}
// 更新配置
Properties.Settings.Default["vfsfile"] = dialog.FileName;
Properties.Settings.Default.Save();
}
}
}
else
{
device = new FileAdapter((String)Properties.Settings.Default["vfsfile"], 1 << 10 << 10 << 10);
}
vfs = new VFS(device);
if (!vfs.IsFormated())
{
var result = System.Windows.Forms.MessageBox.Show("该磁盘镜像文件尚未格式化,是否格式化?", "VFS", System.Windows.Forms.MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.No)
{
Close();
return;
}
vfs.Format(device.Size() >> 10, 4);
// 写入一个示例文件
var file = vfs.NewFile("/README.txt", VFS.FileMode.Create);
file.Write(Encoding.UTF8.GetBytes("Hello world!\r\n\r\n这个文件是格式化时自动建立的,如果你看到了这个文件,说明一切工作正常。\r\n\r\n当你使用记事本修改这个文件并保存以后,它会同步到虚拟文件系统中。\r\n\r\n该虚拟文件系统是索引文件系统,inode 默认占总空间的 10%,理论可支持单一文件最大 2GB。"));
System.Windows.Forms.MessageBox.Show("磁盘格式化成功!", "VFS", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
LoadDirectory("/");
UpdateInfo();
}