10

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
                );
}
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • What do you mean Stream Philip? you want to stream that file to another client? or just open the file for access content of it? – Ali Jun 15 '17 at 22:03
  • @combo_ci I just want to open the file to access the contents, see above. I updated my post. – Philip Tenn Jun 15 '17 at 22:06
  • Possible duplicate of [How to read embedded resource text file](https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Clint Jun 15 '17 at 22:23

2 Answers2

10

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

using (var stream = assembly.GetManifestResourceStream("Project.Resources.client_secret.json"))
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
6

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;
Ali
  • 3,373
  • 5
  • 42
  • 54