Ejemplo n.º 1
0
        public Stream GetStream(string path, ResourceSource sources = ResourceSource.Embedded)
        {
            if (Path.IsPathRooted(path))
            {
                if (sources.HasFlag(ResourceSource.Absolute))
                {
                    if (File.Exists(path))
                    {
                        return(new FileStream(path, FileMode.Open, FileAccess.Read));
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Resource paths must be relative ({path})");
                }
            }
            else
            {
                if (sources.HasFlag(ResourceSource.Relative))
                {
                    var combinedPath = basePath != null?
                                       Path.Combine(basePath, path) : path;

                    if (File.Exists(combinedPath))
                    {
                        return(new FileStream(combinedPath, FileMode.Open, FileAccess.Read));
                    }
                }

                if (sources.HasFlag(ResourceSource.Embedded))
                {
                    var resourceName = $"{baseNamespace}.{path.Replace('\\', '.').Replace('/', '.')}";
                    var stream       = assembly.GetManifestResourceStream(resourceName);
                    if (stream != null)
                    {
                        return(stream);
                    }
                }
            }

            Trace.WriteLine($"Not found: {path} ({sources})", "Resources");
            return(null);
        }