C# 从本地文件夹读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2291835/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:08:17  来源:igfitidea点击:

Read a text file from local folder

c#filefile-io

提问by Dabiddo

I want to read a text file from my local directory, I added the text file to my c# solution, so it would get copied at deployment.. but how do i open it? I've been searching but all the examples assume I have a C:\textfile.txt:

我想从我的本地目录中读取一个文本文件,我将该文本文件添加到我的 c# 解决方案中,所以它会在部署时被复制..但是我如何打开它?我一直在搜索,但所有的例子都假设我有一个 C:\textfile.txt:

I tried just reading the file

我试着只读取文件

if (File.Exists("testfile.txt"))
{
   return true;
}

That didn't work. Then I tried:

那没有用。然后我尝试:

if (File.Exists(@"\TextConsole\testfile.txt"))
{
   return true;
}

but still wont open it.. any ideas??

但仍然不会打开它..有什么想法吗??

采纳答案by Stan R.

Just because you added it to your solution doesn't mean the file gets placed into your output Build directory. If you want to use relative path, make sure your TextFile is copied during build to the output directory. To do this, in solution explorer go to properties of the text file and set Copy to Output Directoryto Alwaysor Copy if newer

仅仅因为您将它添加到您的解决方案中并不意味着该文件会被放置到您的输出 Build 目录中。如果要使用相对路径,请确保在构建期间将 TextFile 复制到输出目录。为此,在解决方案资源管理器中转到文本文件的属性并设置Copy to Output DirectoryAlwaysCopy if newer

Then you can use

然后你可以使用

File.Open("textfile.txt");

回答by Asad

you need to use one of the following after the check you have made

在您进行检查后,您需要使用以下其中一项

 string path = @"\TextConsole\testfile.txt";
 if (File.Exists(path))
 {
  FileStream fileStream = File.OpenRead(path); // or
  TextReader textReader = File.OpenText(path); // or
  StreamReader sreamReader = new StreamReader(path);
 }

回答by Oded

If the file is indeed in c:\textfile.txt, you can find it like this:

如果文件确实在 中c:\textfile.txt,您可以像这样找到它:

if (File.Exists(@"c:\testfile.txt"))
{
   return true;
}

But you should use Path.Combineto build a nested file path and DriveInfoto work with drive details.

但是您应该使用它Path.Combine来构建嵌套文件路径并DriveInfo处理驱动器详细信息。

回答by Kamruzzaman

This example reads the contents of a text file, one line at a time, into a string using the ReadLine method of the StreamReader class. Each text line is stored into the string line and displayed on the screen.

此示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容(一次一行)读入字符串。每个文本行都存储到字符串行中并显示在屏幕上。

  int counter = 0;
  string line;

// Read the file and display it line by line.
System.IO.StreamReader file =  new System.IO.StreamReader("c:\test.txt");

while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();

reference http://msdn.microsoft.com/en-us/library/aa287535%28v=vs.71%29.aspx

参考http://msdn.microsoft.com/en-us/library/aa287535%28v=vs.71%29.aspx

回答by CPHPython

As Bobby mentioned in a comment, using a simple PathCombinein the current folder worked for me:

正如鲍比在评论中提到的PathCombine在当前文件夹中使用一个简单的方法对我有用:

string txtPath = Path.Combine(Environment.CurrentDirectory, "testfile.txt")