2017-06-15 154 views
1

我有一个嵌入的资源文件:.NET获取嵌入的资源文件

enter image description here

我需要打开它作为一个流。

我已经试过(没有工作,流为null):

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

任何意见或建议?

编辑:我在做什么吧:

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 
       ); 
} 
+0

你是什么意思菲利普?你想将该文件传输到另一个客户端?或者只是打开文件来访问它的内容? –

+0

@combo_ci我只想打开文件来访问内容,请参阅上文。我更新了我的帖子。 –

+0

[如何阅读嵌入式资源文本文件]可能的重复(https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Clint

回答

2

我解决了这个,我不得不提供资源的完整的命名空间:

using (var stream = assembly.GetManifestResourceStream("MunicipalAgenda.Resources.client_secret.json")) 
1

右键单击您的项目在解决方案资源管理器 - >添加 - >新建项目 - >资源文件

然后双击创建的文件(例如Resource1.resx),然后添加您的client_secret.json它。现在您可以使用吹码访问client_secret.json内容。请注意,如果你把一个JSON文件的资源,当得到client_secret你会得到一个byte []和必须convert that to string

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

但如果只添加.txt文件,您可以访问该文件与:

var foo= Resource1.txtfile;