Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Get embedded Resource File

I have an embedded Resource File:

enter image description here

I need to open it as a Stream.

What I've tried (did not work, stream is null):

var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("client_secret.json"))

Any ideas or suggestions?

Edit: What I'm doing with it:

using (var stream = assembly.GetManifestResourceStream("client_secret.json"))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                  new[] { YouTubeService.Scope.YoutubeUpload },
                  "user",
                  CancellationToken.None
                );
}
like image 731
Philip Tenn Avatar asked Jun 15 '17 22:06

Philip Tenn


People also ask

What is embedded resource C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file. Advantages.

What is .resx file in C#?

A . resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.

How do I open a RESX file in Visual Studio?

Open a . resx file in the XML (Text) editor. Press Ctrl+Alt+F or choose ReSharper | Windows | File Structure from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there.


2 Answers

I solved this, I had to provide the full Namespace of the Resource:

using (var stream = assembly.GetManifestResourceStream("Project.Resources.client_secret.json"))
like image 140
Philip Tenn Avatar answered Sep 22 '22 12:09

Philip Tenn


Right-Click your Project in Solution Explorer -> Add -> New Item -> Resources File

Then double-click on the created file (e.g. Resource1.resx), and then add your client_secret.json to it. Now you can access to client_secret.json content with blow code. Note that if you put a json file to resource, when get the client_secret you get a byte[] and must convert that to string

 var foo= Encoding.UTF8.GetString(Resource1.client_secret);

But if only add .txt file you can access to that file with:

var foo= Resource1.txtfile;
like image 39
Ali Avatar answered Sep 20 '22 12:09

Ali