我希望我做错了什么,因为此刻对我来说,我所做的似乎是正确的。首先,我想使用命名空间打开一个文件。
假设我有这个字符串:
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)是静态的。
所以我的问题:
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.
您已经排除了某些情况,因此您应该检查应用程序是否对目标文件或其父目录具有适当的读取权限。
从程序集读取
资源流的问题通常与:
您可以使用以下命令列出程序集中的所有可用资源:
Assembly.GetExecutingAssembly().GetManifestResourceNames()
关于使用命名空间找不到 c#asp.net web api 文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684765/