/// <summary> /// Fetches the file content from the provided <paramref name="uri"/> as a <see cref="ContentResourceInfo"/>. /// </summary> /// <param name="uri">The URI of the content.</param> /// <returns>A <see cref="ContentResourceInfo"/> containing the content from the <paramref name="uri"/>.</returns> public static ContentResourceInfo ResolveFromFile(Uri uri) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } if (uri.IsAbsoluteUri && !uri.IsFile) { throw new ArgumentException("Absolute uri is not in a file form."); } Assembly assembly = Assembly.GetEntryAssembly(); if (!uri.IsAbsoluteUri && assembly != null) // Relative to current directory { string currentDirectory = Path.GetDirectoryName(assembly.Location) + "\\"; Uri baseUri = new Uri(currentDirectory); uri = new Uri(baseUri, uri); } WebRequest webRequest = WebRequest.Create(uri); WebResponse webResponse = webRequest.GetResponse(); ContentResourceInfo contentResourceInfo = new ContentResourceInfo(webResponse.GetResponseStream(), webResponse.ContentType, UriResolutionType.File); return(contentResourceInfo); }
private static ContentResourceInfo ResolveByTrial(Uri uri, params UriResolutionType[] trialResolutionTypes) { ContentResourceInfo contentResourceInfo = null; foreach (UriResolutionType trialResolutionType in trialResolutionTypes) { try { contentResourceInfo = trialResolutionType switch { UriResolutionType.Web => ResolveFromWeb(uri), UriResolutionType.File => ResolveFromFile(uri), UriResolutionType.PackageResource => ResolveFromPackageResource(uri), UriResolutionType.PackageContent => ResolveFromPackageContent(uri), UriResolutionType.PackageSiteOfOrigin => ResolveFromPackageSiteOfOrigin(uri) }; if (contentResourceInfo != null) { break; } } catch { /* Ignored */ } } return(contentResourceInfo); }
/// <summary> /// Fetches the package resource content from the provided <paramref name="uri"/> as a <see cref="ContentResourceInfo"/>. /// </summary> /// <param name="uri">The URI of the content.</param> /// <returns>A <see cref="ContentResourceInfo"/> containing the content from the <paramref name="uri"/>. Returns null if content is not found.</returns> public static ContentResourceInfo ResolveFromPackageResource(Uri uri) { StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uri); ContentResourceInfo contentResourceInfo = null; if (streamResourceInfo != null) { contentResourceInfo = new ContentResourceInfo(streamResourceInfo, UriResolutionType.PackageResource); } return(contentResourceInfo); }
/// <summary> /// Fetches the web content from the provided <paramref name="uri"/> as a <see cref="ContentResourceInfo"/>. /// </summary> /// <param name="uri">The URI of the content.</param> /// <returns>A <see cref="ContentResourceInfo"/> containing the content from the <paramref name="uri"/>.</returns> public static ContentResourceInfo ResolveFromWeb(Uri uri) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } WebRequest webRequest = WebRequest.Create(uri); WebResponse webResponse = webRequest.GetResponse(); ContentResourceInfo contentResourceInfo = new ContentResourceInfo(webResponse.GetResponseStream(), webResponse.ContentType, UriResolutionType.Web); return(contentResourceInfo); }