使用命名空间找不到 c#asp.net web api 文件存在

标签 c# file asp.net-web-api stream namespaces

我希望我做错了什么,因为此刻对我来说,我所做的似乎是正确的。首先,我想使用命名空间打开一个文件。
假设我有这个字符串:

string resource = "Middelware.DataResources.dba.Queries.getTest.sql"

然后我尝试使用以下方法打开它:
var stm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)

但不幸的是它不起作用。所以我一直使用它作为测试用例:
if (File.Exists(@"C:\text.txt"))

但由于某种原因,它一直返回 false。我也喜欢使用:“C:\\text.txt”。但什么也没有发生。

我正在使用 Visual Studio 2017。而发生这种情况的类(class)是静态的。

所以我的问题:
  • 为什么不能 file.exist 似乎验证测试文件存在?
  • 为什么是
    var stm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource) 不起作用,因为命名空间应该是正确的?

  • 希望有人能帮我解决这个愚蠢的问题。

    亲切的问候

    最佳答案

    你没有提供太多信息,所以我猜。
    从文件中读取
    来自 File.Exists method reference ,返回值可以是:

    true if the caller has the required permissions and path contains the name of an existing file; otherwise, false.

    This method also returns false if path is null, an invalid path, or a zero-length string.

    If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.


    您已经排除了某些情况,因此您应该检查应用程序是否对目标文件或其父目录具有适当的读取权限。
    从程序集读取
    资源流的问题通常与:
  • 错误的资源名称。可以是大写/小写,也可以是 Visual Studio 编译器作为前缀的附加字符串。
  • 资源实际上并未在程序集中编译

  • 您可以使用以下命令列出程序集中的所有可用资源:
    Assembly.GetExecutingAssembly().GetManifestResourceNames()
    

    关于使用命名空间找不到 c#asp.net web api 文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684765/

    相关文章:

    c# - 使用 LINQ 交错多个(超过 2 个)不规则列表

    c# - 删除特定 API 调用的授权规则

    java - 如何写入文件并添加 "' 和 )

    Windows BAT : test if a specific file is empty

    java - 为什么此 Java 代码使用 BufferedReader 与 String split 不同\r\n

    url - 我可以在 URL 中使用 '|' 吗?

    azure - Azure 应用服务上带有 OWIN 'SystemWeb' 的 Web API 应用程序

    c# - 参数化查询需要未提供的参数 p1

    c# - 如何为子属性在 Style、Setter 和 HierarchicalDataTemplate 上设置 DataContext 绑定(bind)?

    c# - sitecore 插入项目数据完整性