Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Resourse file using Assembly GetManifestResourceStream method using C#

I'm having a Certificate under root folder of the project. The Project name is SingleSignOn, but I can't able to read the method using the GetManifestResourceStream build-in method.

The Source Code is

namespace SingleSignOn
{
    public class Program
    {
        static void Main(string[] args)
         {
            var assembly = typeof(Program).Assembly;
            var super = assembly.GetManifestResourceNames();
            using (var stream = assembly.GetManifestResourceStream("SingleSignOn.idsrv3test.pfx"))
            {

            }
        }
    }
}

Snap shot of Solution Explorer enter image description here

I'm getting NULL from the said built-in method GetManifestResourceStream

enter image description here

I don't know what I missed in this. The URL of the said Certificate is https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Certificates/idsrv3test.pfx

Kindly assist me how to read the Certificate.

like image 318
B.Balamanigandan Avatar asked Nov 30 '25 11:11

B.Balamanigandan


2 Answers

Try this:

var resourceName = "SingleSignOn.idsrv3test.pfx";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
  TextReader tr = new StreamReader(stream);
  string fileContents = tr.ReadToEnd();
}

Note: Put the file as an embedded resource. file > right click > properties > build action > select embedded resource.

like image 85
Just code Avatar answered Dec 01 '25 23:12

Just code


Solution 2

Step 1: Change properties of .PFX file idsrv3test.pfx properties, set Build action as Embedded Resource.

Step 2: Code change:

var resourceName = "SingleSignOn.idsrv3test.pfx";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
  TextReader tr = new StreamReader(stream);
  string fileContents = tr.ReadToEnd();
}
like image 33
Shailesh Maniyar Avatar answered Dec 02 '25 00:12

Shailesh Maniyar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!