C# Streamreader 到相对文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10623656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Streamreader to a relative filepath
提问by gfppaste
I was wondering, if anyone could tell me how to point a StreamReader to a file inside the current working directory of the program.
我想知道,是否有人能告诉我如何将 StreamReader 指向程序当前工作目录中的文件。
E.g.: say I have program Prog saved in the directory "C:\ProgDir\". I commit "\ProgDir" to a shared folder. Inside ProgDir is another directory containing files I'd like to import into Prog (e.g. "\ProgDir\TestDir\TestFile.txt") I'd like to make it so that the StreamReader could read those TestFiles, even when the path to the directory has changed;
例如:假设我将程序 Prog 保存在目录“C:\ProgDir\”中。我将“\ProgDir”提交到共享文件夹。ProgDir 里面是另一个包含我想导入到 Prog 中的文件的目录(例如“\ProgDir\TestDir\TestFile.txt”)我想让它这样 StreamReader 可以读取这些 TestFiles,即使是目录已更改;
(E.G., on my computer, the path to the Testfiles is
(例如,在我的电脑上,Testfiles 的路径是
C:\ProgDir\TestDir\TestFile.txt
C:\ProgDir\TestDir\TestFile.txt
but on the other person's computer, the directory is
但是在另一个人的电脑上,目录是
C:\dev_code\ProgDir\TestDir\TestFile.txt
C:\dev_code\ProgDir\TestDir\TestFile.txt
).
)。
How would I get a StreamReader to be ale to read from TestFile.txt on the other person's computer? (to clarify, the filenames do not change, the only change is the path ProgDir)
我如何让 StreamReader 能够从其他人的计算机上的 TestFile.txt 读取?(澄清一下,文件名不会改变,唯一的变化是路径 ProgDir)
I tried the following:
我尝试了以下方法:
string currentDir = Environment.CurrentDirectory;
DirectoryInfo directory = new DirectoryInfo(currentDir);
FileInfo file = new FileInfo("TestFile.txt");
string fullDirectory = directory.FullName;
string fullFile = file.FullName;
StreamReader sr = new StreamReader(@fullDirectory + fullFile);
( pulled this from : Getting path relative to the current working directory?)
(从:获取相对于当前工作目录的路径?)
But I'm getting "TestFile does not exist in the current context". Anyone have any idea as to how I should approach this?
但是我得到“当前上下文中不存在 TestFile”。任何人都知道我应该如何处理这个问题?
Thank you.
谢谢你。
采纳答案by hjgraca
Is the Folder "TestDir" always in the executable directory? if so, try this
文件夹“TestDir”是否总是在可执行目录中?如果是这样,试试这个
string dir =System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
string file = dir + @"\TestDir\TestFile.txt";
This will give you the path of the exe plus the folder inside it and the textfile
这将为您提供 exe 的路径以及其中的文件夹和文本文件
回答by RajN
You can use the GetFullPath()method. Try this:
您可以使用该GetFullPath()方法。尝试这个:
string filePath = System.IO.Path.GetFullPath("TestFile.txt");
StreamReader sr = new StreamReader(filePath);
回答by Tester101
The FileInfoconstructortakes a single parameter of type string. Try putting quotes around TestFile.txt.
该FileInfo的构造采用字符串类型的单个参数。尝试在 TestFile.txt 周围加上引号。
Change
改变
FileInfo file = new FileInfo(TestFile.txt);
FileInfo file = new FileInfo(TestFile.txt);
to
到
FileInfo file = new FileInfo("TestFile.txt");
FileInfo file = new FileInfo("TestFile.txt");
Unless TestFile is an object with a property named txtof type string, in which case you have to create the object before trying to use it.
除非 TestFile 是一个具有名为txtstring 类型的属性的对象,在这种情况下,您必须在尝试使用它之前创建该对象。
回答by SPFiredrake
A few things:
一些东西:
First, FileInfo.FullNamegives the absolute path for the file, so you don't need to prepend the full directory path before the file in the StreamReader instance.
首先,FileInfo.FullName给出文件的绝对路径,因此您不需要在 StreamReader 实例中的文件之前添加完整的目录路径。
Second, FileInfo file = new FileInfo(TestFile.txt);should fail unless you actually have a class called TestFilewith a txtproperty.
其次,FileInfo file = new FileInfo(TestFile.txt);应该失败,除非您确实有一个TestFile使用txt属性调用的类。
Finally, with almost every Filemethod, they use relative paths already. So you SHOULD be able to use the stream reader on JUST the relative path.
最后,对于几乎所有File方法,它们都已经使用了相对路径。所以你应该能够在相对路径上使用流阅读器。
Give those few things a try and let us know.
试试这几件事,让我们知道。
Edit: Here's what you should try:
编辑:这是你应该尝试的:
FileInfo file = new FileInfo("TestFile.txt");
StreamReader sr = new StreamReader(fullFile.FullName);
//OR
StreamReader sr = new StreamReader("TestFile.txt");
However, one thing I noticed is that the TestFile is located in TestDir. If your executable is located in ProgDiras you're stating, then this will still fail because your relative path isn't right.
但是,我注意到的一件事是 TestFile 位于TestDir. 如果您的可执行文件位于ProgDir您所说的位置,那么这仍然会失败,因为您的相对路径不正确。
Try changing it to TestDir\TestFile.txtinstead. IE: StreamReader sr = new StreamReader("TestDir\TestFile.txt");
尝试将其更改为TestDir\TestFile.txt。IE:StreamReader sr = new StreamReader("TestDir\TestFile.txt");
回答by jorence jovin
You can use path.combine to get the current directory to build and then combine the file path you need
可以使用 path.combine 获取当前目录进行构建,然后组合你需要的文件路径
new StreamReader(Path.Combine(Environment.CurrentDirectory, "storage"));
回答by Andreas
The easiest way would be to just use the file name (not the full path) and "TestDir" and give the StreamReader a relative path.
最简单的方法是只使用文件名(而不是完整路径)和“TestDir”并为 StreamReader 提供一个相对路径。
var relativePath = Path.Combine(".","TestDir",fileName);
using (var sr = new StreamReader(relativePath))
{
//...
}

