public static IByteDevice Create(Uri.Locator resource) { IByteDevice result = ByteDevice.Open(resource, System.IO.FileMode.Create); if (result.IsNull() && resource.NotNull()) { System.IO.Directory.CreateDirectory(resource.Path.FolderPath.PlatformPath); result = ByteDevice.Open(resource, System.IO.FileMode.Create); } return result; }
static IByteDevice Open(Uri.Locator resource, System.IO.FileMode mode) { IByteDevice result = null; if (resource.NotNull()) switch (resource.Scheme) { case "assembly": result = resource.Authority == "" ? ByteDevice.Open(System.Reflection.Assembly.GetEntryAssembly(), resource.Path) : ByteDevice.Open(System.Reflection.Assembly.Load(new System.Reflection.AssemblyName(resource.Authority)), resource.Path); break; case "file": try { System.IO.FileStream stream = System.IO.File.Open(System.IO.Path.GetFullPath(resource.PlatformPath), mode, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite); if (stream.NotNull()) result = new ByteDevice(stream) { Resource = resource, FixedLength = true }; } catch (System.IO.DirectoryNotFoundException) { result = null; } catch (System.IO.FileNotFoundException) { result = null; } break; case "http": case "https": if (mode == System.IO.FileMode.Open) { try { using (System.Net.WebClient client = new System.Net.WebClient()) result = new ByteDevice(new System.IO.MemoryStream(client.DownloadData(resource))) { Resource = resource, FixedLength = true }; } catch (System.Net.WebException) { result = null; } } break; } return result; }