public static StreamReader EmbeddedResourceStreamReader(string resourceId, Assembly assembly = null)
 {
     if (EmbeddedResource.GetStream(resourceId, assembly) is Stream stream)
     {
         return(new StreamReader(stream));
     }
     return(null);
 }
        public static string EmbeddedStoredText(string resourceId, Assembly assembly = null)
        {
            string contents = null;

            using (Stream stream = EmbeddedResource.GetStream(resourceId, assembly))
            {
                if (stream != null)
                {
                    using (var reader = new StreamReader(stream))
                    {
                        contents = reader.ReadToEnd();
                    }
                }
            }
            return(contents);
        }
예제 #3
0
#pragma warning disable CS1998
        static async Task <bool> CacheTask(string resourceId, Assembly assembly, string path)
#pragma warning restore CS1998
        {
            try
            {
                using (var stream = EmbeddedResource.GetStream(resourceId, assembly))
                {
                    if (stream is null)
                    {
                        Console.WriteLine("Cannot find EmbeddedResource [" + resourceId + "] in assembly [" + assembly.FullName + "].   Here are the ResourceIds in that assembly:");
                        foreach (var id in assembly.GetManifestResourceNames())
                        {
                            Console.WriteLine("\t" + id);
                        }
                        Console.WriteLine("");
                    }
                    else
                    {
                        //if (File.Exists(path))
                        //    System.Diagnostics.Debug.WriteLine("DownloadTask: FILE ALREADY EXISTS [" + path + "] [" + assembly.GetName().Name + ";" + resourceId + "]");

                        using (var fileStream = new FileStream(path, FileMode.Create))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            stream.CopyTo(fileStream);
                            fileStream.Flush(true);
                            var length = fileStream.Length;
                            //System.Diagnostics.Debug.WriteLine("DownloadTask: file written [" + path + "] [" + assembly.GetName().Name + ";" + resourceId + "] length=[" + length + "] name=[" + fileStream.Name + "] pos=[" + fileStream.Position + "]");
                        }
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            return(false);
        }