阅读从本地文件夹中的文本文件 [英] Read a text file from local folder

查看:161
本文介绍了阅读从本地文件夹中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

我尝试只是读取文件

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??

推荐答案

仅仅因为你把它添加到您的解决方案并不意味着文件中获取的放入您的输出build目录。如果你要使用相对路径,确保构建的输出目录中的文本文件被复制。要做到这一点,在解决方案资源管理器去到文本文件的属性,并设置复制到输出目录总是复制如果新

Just because you added it to your solution doesn't mean the file get's 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 Directory to Always or Copy if newer

然后你可以使用

File.Open("textfile.txt");

这篇关于阅读从本地文件夹中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆