public void Exists() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"dir\foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); Assert.IsFalse(fs.GetFileInfo("unknown.txt").Exists); Assert.IsTrue(fs.GetFileInfo(@"dir\foo.txt").Exists); Assert.IsFalse(fs.GetFileInfo(@"dir").Exists); }
public void Length() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"FILE.TXT", new byte[0]); builder.AddFile(@"FILE2.TXT", new byte[1]); builder.AddFile(@"FILE3.TXT", new byte[10032]); builder.AddFile(@"FILE3.TXT;2", new byte[132]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(0, fs.GetFileInfo("FILE.txt").Length); Assert.AreEqual(1, fs.GetFileInfo("FILE2.txt").Length); Assert.AreEqual(10032, fs.GetFileInfo("FILE3.txt;1").Length); Assert.AreEqual(132, fs.GetFileInfo("FILE3.txt;2").Length); Assert.AreEqual(132, fs.GetFileInfo("FILE3.txt").Length); }
public void AddFileBytes() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"ADIR\AFILE.TXT", new byte[] {}); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.Exists(@"ADIR\AFILE.TXT")); }
public void AddFileStream() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"ADIR\AFILE.TXT", new MemoryStream()); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.Exists(@"ADIR\AFILE.TXT")); }
public void GetFileSystemInfos() { CDBuilder builder = new CDBuilder(); builder.AddDirectory(@"SOMEDIR\CHILD\GCHILD"); builder.AddDirectory(@"AAA.EXT"); builder.AddFile(@"FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", new byte[10]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(3, fs.Root.GetFileSystemInfos().Length); Assert.AreEqual(1, fs.Root.GetFileSystemInfos("*.EXT").Length); Assert.AreEqual(2, fs.Root.GetFileSystemInfos("*.?XT").Length); }
public void AddFileStream() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"ADIR\AFILE.TXT", new MemoryStream()); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.Exists(@"ADIR\AFILE.TXT")); }
public void CreationTimeUtc() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); Assert.GreaterOrEqual(DateTime.UtcNow, fs.GetFileInfo("foo.txt").CreationTimeUtc); Assert.LessOrEqual(DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10)), fs.GetFileInfo("foo.txt").CreationTimeUtc); }
public void AddFileBytes() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"ADIR\AFILE.TXT", new byte[] {}); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.Exists(@"ADIR\AFILE.TXT")); }
public void Exists() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"dir\foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); Assert.IsFalse(fs.GetFileInfo("unknown.txt").Exists); Assert.IsTrue(fs.GetFileInfo(@"dir\foo.txt").Exists); Assert.IsFalse(fs.GetFileInfo(@"dir").Exists); }
public void CreationTimeUtc() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); Assert.GreaterOrEqual(DateTime.UtcNow, fs.GetFileInfo("foo.txt").CreationTimeUtc); Assert.LessOrEqual(DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10)), fs.GetFileInfo("foo.txt").CreationTimeUtc); }
public void Attributes() { CDBuilder builder = new CDBuilder(); builder.AddFile("foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo fi = fs.GetFileInfo("foo.txt"); // Check default attributes Assert.AreEqual(FileAttributes.ReadOnly, fi.Attributes); }
public void GetFiles() { CDBuilder builder = new CDBuilder(); builder.AddDirectory(@"SOMEDIR\CHILD\GCHILD"); builder.AddDirectory(@"AAA.DIR"); builder.AddFile(@"FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", new byte[10]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(1, fs.Root.GetFiles().Length); Assert.AreEqual("FOO.TXT", fs.Root.GetFiles()[0].FullName); Assert.AreEqual(2, fs.Root.GetDirectories("SOMEDIR")[0].GetFiles("*.TXT").Length); Assert.AreEqual(4, fs.Root.GetFiles("*.TXT", SearchOption.AllDirectories).Length); Assert.AreEqual(0, fs.Root.GetFiles("*.DIR", SearchOption.AllDirectories).Length); }
public void SimpleSearch() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\CHILDDIR\GCHILDIR\FILE.TXT", new byte[0]); CDReader fs = new CDReader(builder.Build(), false); DiscDirectoryInfo di = fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR"); DiscFileInfo[] fis = di.GetFiles("*.*", SearchOption.AllDirectories); }
public void Parent() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\ADIR\FILE.TXT", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo fi = fs.GetFileInfo(@"SOMEDIR\ADIR\FILE.TXT"); Assert.AreEqual(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Parent); Assert.AreEqual(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Directory); }
public void Attributes() { CDBuilder builder = new CDBuilder(); builder.AddFile("foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo fi = fs.GetFileInfo("foo.txt"); // Check default attributes Assert.AreEqual(FileAttributes.ReadOnly, fi.Attributes); }
public void Exists() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\CHILDDIR\FILE.TXT", new byte[0]); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.GetDirectoryInfo(@"\").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR\").Exists); Assert.IsFalse(fs.GetDirectoryInfo(@"NONDIR").Exists); Assert.IsFalse(fs.GetDirectoryInfo(@"SOMEDIR\NONDIR").Exists); }
public void Exists() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\CHILDDIR\FILE.TXT", new byte[0]); CDReader fs = new CDReader(builder.Build(), false); Assert.IsTrue(fs.GetDirectoryInfo(@"\").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR").Exists); Assert.IsTrue(fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR\").Exists); Assert.IsFalse(fs.GetDirectoryInfo(@"NONDIR").Exists); Assert.IsFalse(fs.GetDirectoryInfo(@"SOMEDIR\NONDIR").Exists); }
public void BuildISO(Stream ISOOpen, string pathSave) { CDReader cd = new CDReader(ISOOpen, true); CDBuilder cb = new CDBuilder(); cb.UseJoliet = true; foreach (DiscFileInfo file in cd.Root.GetFiles("*.*", SearchOption.AllDirectories)) { foreach (DiscDirectoryInfo dir in cd.Root.GetDirectories("*.*", SearchOption.AllDirectories)) { cb.AddDirectory(dir.Name); cb.AddFile(file.FullName, cd.OpenFile(file.FullName, FileMode.Open)); } } cb.Build(pathSave); }
public void LargeDirectory() { CDBuilder builder = new CDBuilder(); builder.UseJoliet = true; for (int i = 0; i < 3000; ++i) { builder.AddFile("FILE" + i + ".TXT", new byte[] { }); } CDReader reader = new CDReader(builder.Build(), true); Assert.AreEqual(3000, reader.Root.GetFiles().Length); }
public void HideVersions() { CDBuilder builder = new CDBuilder(); builder.UseJoliet = true; builder.AddFile("FILE.TXT;1", new byte[] { }); MemoryStream ms = new MemoryStream(); SparseStream.Pump(builder.Build(), ms); CDReader reader = new CDReader(ms, true, false); Assert.AreEqual("\\FILE.TXT;1", reader.GetFiles("")[0]); Assert.AreEqual("\\FILE.TXT;1", reader.GetFileSystemEntries("")[0]); reader = new CDReader(ms, true, true); Assert.AreEqual("\\FILE.TXT", reader.GetFiles("")[0]); Assert.AreEqual("\\FILE.TXT", reader.GetFileSystemEntries("")[0]); }
public void Open_Read() { CDBuilder builder = new CDBuilder(); builder.AddFile("foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo di = fs.GetFileInfo("foo.txt"); using (Stream s = di.Open(FileMode.Open, FileAccess.Read)) { Assert.IsFalse(s.CanWrite); Assert.IsTrue(s.CanRead); Assert.AreEqual(1, s.ReadByte()); } }
public void HideVersions() { CDBuilder builder = new CDBuilder(); builder.UseJoliet = true; builder.AddFile("FILE.TXT;1", new byte[] { }); MemoryStream ms = new MemoryStream(); SparseStream.Pump(builder.Build(), ms); CDReader reader = new CDReader(ms, true, false); Assert.AreEqual("\\FILE.TXT;1", reader.GetFiles("")[0]); Assert.AreEqual("\\FILE.TXT;1", reader.GetFileSystemEntries("")[0]); reader = new CDReader(ms, true, true); Assert.AreEqual("\\FILE.TXT", reader.GetFiles("")[0]); Assert.AreEqual("\\FILE.TXT", reader.GetFileSystemEntries("")[0]); }
private static void PopulateFromFolder(CDBuilder builder, DirectoryInfo di, string basePath) { foreach (FileInfo file in di.GetFiles()) { builder.AddFile(file.FullName.Substring(basePath.Length), file.FullName); } foreach (DirectoryInfo dir in di.GetDirectories()) { PopulateFromFolder(builder, dir, basePath); } }
public void Open_Read() { CDBuilder builder = new CDBuilder(); builder.AddFile("foo.txt", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo di = fs.GetFileInfo("foo.txt"); using (Stream s = di.Open(FileMode.Open, FileAccess.Read)) { Assert.IsFalse(s.CanWrite); Assert.IsTrue(s.CanRead); Assert.AreEqual(1, s.ReadByte()); } }
public void GetFileSystemInfos() { CDBuilder builder = new CDBuilder(); builder.AddDirectory(@"SOMEDIR\CHILD\GCHILD"); builder.AddDirectory(@"AAA.EXT"); builder.AddFile(@"FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", new byte[10]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(3, fs.Root.GetFileSystemInfos().Length); Assert.AreEqual(1, fs.Root.GetFileSystemInfos("*.EXT").Length); Assert.AreEqual(2, fs.Root.GetFileSystemInfos("*.?XT").Length); }
public void Parent() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\ADIR\FILE.TXT", new byte[] { 1 }); CDReader fs = new CDReader(builder.Build(), false); DiscFileInfo fi = fs.GetFileInfo(@"SOMEDIR\ADIR\FILE.TXT"); Assert.AreEqual(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Parent); Assert.AreEqual(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Directory); }
public override bool Execute() { Log.LogMessage(MessageImportance.Normal, "Creating ISO file: '{0}'", FileName.ItemSpec); try { CDBuilder builder = new CDBuilder(); builder.UseJoliet = UseJoliet; if (!string.IsNullOrEmpty(VolumeLabel)) { builder.VolumeIdentifier = VolumeLabel; } Stream bootImageStream = null; if (BootImage != null) { bootImageStream = new FileStream(BootImage.GetMetadata("FullPath"), FileMode.Open, FileAccess.Read); builder.SetBootImage(bootImageStream, BootDeviceEmulation.NoEmulation, 0); builder.UpdateIsolinuxBootTable = UpdateIsolinuxBootTable; } foreach (var sourceFile in SourceFiles) { builder.AddFile(GetDestinationPath(sourceFile), sourceFile.GetMetadata("FullPath")); } try { builder.Build(FileName.ItemSpec); } finally { if (bootImageStream != null) { bootImageStream.Dispose(); } } } catch(Exception e) { Log.LogErrorFromException(e, true, true, null); return false; } return !Log.HasLoggedErrors; }
public void SimpleSearch() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"SOMEDIR\CHILDDIR\GCHILDIR\FILE.TXT", new byte[0]); CDReader fs = new CDReader(builder.Build(), false); DiscDirectoryInfo di = fs.GetDirectoryInfo(@"SOMEDIR\CHILDDIR"); DiscFileInfo[] fis = di.GetFiles("*.*", SearchOption.AllDirectories); }
private void PopulateFromFolder(CDBuilder builder, DirectoryInfo di, string basePath, string bootBin) { FileInfo bootBinFile = null; foreach (FileInfo file in di.GetFiles()) { string filePath = file.FullName.Substring(basePath.Length); if (bootBin != null && file.Name.Equals(bootBin, StringComparison.OrdinalIgnoreCase)) { bootBinFile = file; //Ignore this for now, we want it last } else { builder.AddFile(filePath, file.FullName); } } foreach (DirectoryInfo dir in di.GetDirectories()) { PopulateFromFolder(builder, dir, basePath, null); } if (bootBinFile != null && bootBin != null) { builder.AddFile(bootBin, bootBinFile.FullName); long sectorSize = RoundUp(bootBinFile.Length, DATA_SECTOR_SIZE); builder.LastFileStartSector = (uint)(GD_END_LBA - 150 - (sectorSize / DATA_SECTOR_SIZE)); } else if (bootBin != null) { //User doesn't know what they're doing and gave us bad data. throw new FileNotFoundException("IP.BIN requires the boot file " + bootBin + " which was not found in the data directory."); } }
private void CloneCdDirectory(string dir, CDReader cdr, CDBuilder cdb) { foreach (string fileName in cdr.GetFiles(dir)) { if (fileName == "\\reactos\\unattend.inf") continue; var stream = cdr.OpenFile(fileName, FileMode.Open); cdb.AddFile(fileName.Remove(0, 1), stream); stream.Close(); } foreach (string dirName in cdr.GetDirectories(dir)) { CloneCdDirectory(dirName, cdr, cdb); } }
private string Run(int revision, string additionalFilePath, bool autoStartChecked) { string filename = string.Format("bootcd-{0}-dbg", revision); string filename7z = filename + ".7z"; string filenameIso = filename + ".iso"; if (!File.Exists(filenameIso)) { string filename7zTemp = filename7z + ".temp"; string filenameIsoTemp = filenameIso + ".temp"; if (!File.Exists(filename7z)) { File.Delete(filename7zTemp); WebClient wc = new WebClient(); try { wc.DownloadFile(revToUrl[revision], filename7zTemp); } catch (WebException) { return "File download failed:\n '" + revToUrl[revision] + "'"; } File.Move(filename7zTemp, filename7z); } File.Delete(filenameIsoTemp); FileStream isotmpfs = File.Create(filenameIsoTemp); FileStream szfs = File.Open(filename7z, FileMode.Open, FileAccess.Read); SevenZipExtractor sze = new SevenZipExtractor(szfs); sze.ExtractFile(filenameIso, isotmpfs); isotmpfs.Close(); szfs.Close(); File.Move(filenameIsoTemp, filenameIso); File.Delete(filename7z); } string vmName = string.Format("ReactOS_r{0}", revision); string diskName = Environment.CurrentDirectory + "\\" + vmName + "\\" + vmName + ".vdi"; if (File.Exists(diskName)) { FileStream fs = null; try { fs = File.Open(diskName, FileMode.Open, FileAccess.Read, FileShare.None); } catch (IOException) { return "Virtual machine '" + vmName + "' is already running"; } finally { if (fs != null) fs.Close(); } } string filenameIsoUnatt = filename + "_unatt.iso"; string filenameIsoUnattTemp = filenameIsoUnatt + ".temp"; File.Delete(filenameIsoUnattTemp); File.Delete(filenameIsoUnatt); FileStream isofs = File.Open(filenameIso, FileMode.Open, FileAccess.Read); CDReader cdr = new CDReader(isofs, true); CDBuilder cdb = new CDBuilder(); cdb.VolumeIdentifier = cdr.VolumeLabel; CloneCdDirectory("", cdr, cdb); if (!File.Exists("unattend.inf")) File.WriteAllText("unattend.inf", RosRegTest.Resources.unattend, Encoding.ASCII); string additionalFileName = null; if (additionalFilePath != null) additionalFileName = Path.GetFileName(additionalFilePath); string unattText = File.ReadAllText("unattend.inf", Encoding.ASCII); if (autoStartChecked && (additionalFileName != null)) unattText = unattText + "[GuiRunOnce]\n" + "cmd.exe /c start d:\\" + additionalFileName + "\n\n"; cdb.AddFile("reactos\\unattend.inf", Encoding.ASCII.GetBytes(unattText)); if (additionalFileName != null) cdb.AddFile(additionalFileName, additionalFilePath); Stream bootImgStr = cdr.OpenBootImage(); cdb.SetBootImage(bootImgStr, cdr.BootEmulation, cdr.BootLoadSegment); bootImgStr.Close(); cdb.Build(filenameIsoUnattTemp); isofs.Close(); File.Move(filenameIsoUnattTemp, filenameIsoUnatt); string fullIsoName = Environment.CurrentDirectory + "\\" + filenameIsoUnatt; string deleteVmCmd = string.Format("unregistervm --name {0}", vmName); string createVmCmd = string.Format( "createvm --name {0} --basefolder {1} --ostype WindowsXP --register", vmName, Environment.CurrentDirectory); string modifyVmCmd = string.Format("modifyvm {0} --memory 256 --vram 16 --nictype1 Am79C973 --audio none --boot1 disk --boot2 dvd", vmName); string storageCtlCmd = string.Format("storagectl {0} --name \"IDE Controller\" --add ide", vmName); string createMediumCmd = string.Format("createmedium disk --filename {0} --size 2048", diskName); string storageAttachCmd1 = string.Format("storageattach {0} --port 0 --device 0 --storagectl \"IDE Controller\" --type hdd --medium {1}", vmName, diskName); string storageAttachCmd2 = string.Format("storageattach {0} --port 1 --device 0 --storagectl \"IDE Controller\" --type dvddrive --medium {1}", vmName, fullIsoName); string startCmd = string.Format("startvm {0}", vmName); Exec(vboxManagePath, deleteVmCmd); Exec(vboxManagePath, createVmCmd); Exec(vboxManagePath, modifyVmCmd); Exec(vboxManagePath, storageCtlCmd); Exec(vboxManagePath, createMediumCmd); Exec(vboxManagePath, storageAttachCmd1); Exec(vboxManagePath, storageAttachCmd2); Exec(vboxManagePath, startCmd); return null; }
public void LargeDirectory() { CDBuilder builder = new CDBuilder(); builder.UseJoliet = true; for (int i = 0; i < 3000; ++i) { builder.AddFile("FILE" + i + ".TXT", new byte[] { }); } CDReader reader = new CDReader(builder.Build(), true); Assert.AreEqual(3000, reader.Root.GetFiles().Length); }
public void GetFiles() { CDBuilder builder = new CDBuilder(); builder.AddDirectory(@"SOMEDIR\CHILD\GCHILD"); builder.AddDirectory(@"AAA.DIR"); builder.AddFile(@"FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\FOO.TXT", new byte[10]); builder.AddFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", new byte[10]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(1, fs.Root.GetFiles().Length); Assert.AreEqual("FOO.TXT", fs.Root.GetFiles()[0].FullName); Assert.AreEqual(2, fs.Root.GetDirectories("SOMEDIR")[0].GetFiles("*.TXT").Length); Assert.AreEqual(4, fs.Root.GetFiles("*.TXT", SearchOption.AllDirectories).Length); Assert.AreEqual(0, fs.Root.GetFiles("*.DIR", SearchOption.AllDirectories).Length); }
public void Length() { CDBuilder builder = new CDBuilder(); builder.AddFile(@"FILE.TXT", new byte[0]); builder.AddFile(@"FILE2.TXT", new byte[1]); builder.AddFile(@"FILE3.TXT", new byte[10032]); builder.AddFile(@"FILE3.TXT;2", new byte[132]); CDReader fs = new CDReader(builder.Build(), false); Assert.AreEqual(0, fs.GetFileInfo("FILE.txt").Length); Assert.AreEqual(1, fs.GetFileInfo("FILE2.txt").Length); Assert.AreEqual(10032, fs.GetFileInfo("FILE3.txt;1").Length); Assert.AreEqual(132, fs.GetFileInfo("FILE3.txt;2").Length); Assert.AreEqual(132, fs.GetFileInfo("FILE3.txt").Length); }
// https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150630/udp static void Main(string[] args) { // http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7 // "X:\opensource\codeplex\discutils\src\DiscUtils.csproj" // https://github.com/perpetual-motion/discutils // http://technet.microsoft.com/en-us/magazine/ee872416.aspx // http://blogs.technet.com/b/danstolts/archive/2012/11/09/how_2d00_to_2d00_mount_2d00_vhd_2d00_image_2d00_from_2d00_windows_2d00_7_2d00_step_2d00_by_2d00_step_2d00_without_2d00_any_2d00_third_2d00_party_2d00_toolsthe_2d00_easy_2d00_way.aspx CDBuilder builder = new CDBuilder(); builder.UseJoliet = true; builder.VolumeIdentifier = "A_SAMPLE_DISK"; builder.AddFile(@"Folder\Hello.txt", Encoding.ASCII.GetBytes("Hello World!")); //Additional information: The process cannot access the file 'X:\jsc.svn\examples\merge\Test\TestCreateVHD\TestCreateVHD\bin\Debug\sample.iso' because it is being used by another process. builder.Build(@"sample2.iso"); //long diskSize = 4 * 1024 * 1024; //30MB long diskSize = 5 * 1024 * 1024; //30MB // Additional information: Requested size is too small for a partition // sectorCount 8143 int // Comupter Management/Storage/Disk Management/ shows it unallocated! // win7 does not know how to mount vhd? // //Additional information: The process cannot access the file 'X:\jsc.svn\examples\merge\Test\TestCreateVHD\TestCreateVHD\bin\Debug\mydisk.vhd' because it is being used by another process. string fileName = new FileInfo(@"mydisk2.vhd").FullName; using (Stream vhdStream = File.Create(fileName)) { Disk disk = Disk.InitializeDynamic(vhdStream, DiscUtils.Ownership.Dispose, diskSize); BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat); using (FatFileSystem fs = FatFileSystem.FormatPartition(disk, 0, "label1")) { fs.CreateDirectory(@"TestDir\CHILD"); using (var f = new StreamWriter(fs.OpenFile(@"TestDir\Hello.txt", FileMode.OpenOrCreate, FileAccess.Write))) { f.WriteLine("Hello World!"); } // do other things with the file system... } } // http://msdn.microsoft.com/en-us/magazine/dd569754.aspx // http://msdn.microsoft.com/en-us/library/windows/desktop/dd323692(v=vs.85).aspx // http://www.programmershare.com/1846408/ // https://www.jmedved.com/2009/05/open-and-attach/ // http://blogs.msdn.com/b/virtual_pc_guy/archive/2008/01/10/mounting-vhds-from-managed-code.aspx // http://support.microsoft.com/kb/981778 // http://stackoverflow.com/questions/573086/how-to-elevate-privileges-only-when-required // This is not true. You can change the Owner of the process and set the DACL and ACL values for the user giving them administrative powers. IntPtr handle = IntPtr.Zero; // open disk handle var openParameters = new OPEN_VIRTUAL_DISK_PARAMETERS(); openParameters.Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1; openParameters.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT; var openStorageType = new VIRTUAL_STORAGE_TYPE(); openStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_VHD; openStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT; int openResult = OpenVirtualDisk(ref openStorageType, fileName, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, ref openParameters, ref handle); if (openResult != ERROR_SUCCESS) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", openResult)); } // attach disk - permanently var attachParameters = new ATTACH_VIRTUAL_DISK_PARAMETERS(); attachParameters.Version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1; int attachResult = AttachVirtualDisk(handle, IntPtr.Zero, ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, ref attachParameters, IntPtr.Zero); // attachResult = 1314 // If you get “Native error 1314.” exception, you didn’t run code as user with administrative rights. If you get “Native error 32.”, virtual disk is already attached. if (attachResult != ERROR_SUCCESS) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", attachResult)); } // close handle to disk CloseHandle(handle); // http://technet.microsoft.com/en-us/library/dd440865(v=ws.10).aspx System.Windows.Forms.MessageBox.Show("Disk is attached."); }