Пример #1
0
        public static IBlockDevice Create(Uri.Locator resource)
        {
            IBlockDevice result = BlockDevice.Open(resource, System.IO.FileMode.Create);

            if (result.IsNull() && resource.NotNull())
            {
                System.IO.Directory.CreateDirectory(resource.Path.FolderPath.PlatformPath);
                result = BlockDevice.Open(resource, System.IO.FileMode.Create);
            }
            return(result);
        }
Пример #2
0
        static IBlockDevice Open(Uri.Locator resource, System.IO.FileMode mode)
        {
            IBlockDevice result = null;

            if (resource.NotNull())
            {
                switch (resource.Scheme)
                {
                case "assembly":
                    result = resource.Authority == "" ? BlockDevice.Open(System.Reflection.Assembly.GetEntryAssembly(), resource.Path) : BlockDevice.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 BlockDevice(stream, resource);
                        }
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        result = null;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        result = null;
                    }
                    break;

                case "http":
                case "https":
                    if (mode == System.IO.FileMode.Open)
                    {
                        // TODO: support http and https.
                    }
                    break;
                }
            }
            return(result);
        }
Пример #3
0
 public static IBlockDevice Open(Uri.Locator resource)
 {
     return(BlockDevice.Open(resource, System.IO.FileMode.Open));
 }
Пример #4
0
		static IBlockDevice Open(Uri.Locator resource, System.IO.FileMode mode)
		{
			IBlockDevice result = null;
			if (resource.NotNull())
				switch (resource.Scheme)
				{
					case "assembly":
						result = resource.Authority == "" ? BlockDevice.Open(System.Reflection.Assembly.GetEntryAssembly(), resource.Path) : BlockDevice.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 BlockDevice(stream) { Resource = resource }; 
						}
						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 BlockDevice(new System.IO.MemoryStream(client.DownloadData(resource))) {
										Resource = resource,
										FixedLength = true
									};
							}
							catch (System.Net.WebException)
							{
								result = null;
							}
						}
						break;
				}
			return result;
		}