C# 从项目中存在的文件夹中读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13762338/
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
Read files from a Folder present in project
提问by SKJ
I have a C# project (Windows Console Application). I have created a folder named Datainside project. There are two text files inside folder Data.
我有一个 C# 项目(Windows 控制台应用程序)。我在项目中创建了一个名为Data的文件夹。文件夹 Data 中有两个文本文件。
How can I read the text files from "Data" folder. I tried below things.
如何从“Data”文件夹中读取文本文件。我试过下面的事情。
string[] files = File.ReadAllLines(@"Data\Names.txt")
It is thowing error that file not found.
找不到文件是错误的。
I have checked some Stackoverflow answers posted before and none of then are working for me.
我检查了之前发布的一些 Stackoverflow 答案,但没有一个对我有用。
How can I proceed? Thanks!
我该如何继续?谢谢!
采纳答案by Morbia
below code should work:
下面的代码应该工作:
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\Names.txt");
string[] files = File.ReadAllLines(path);
回答by Massimiliano Peluso
it depends where is your Data folder
这取决于您的数据文件夹在哪里
To get the directory where the .exe file is:
要获取 .exe 文件所在的目录:
AppDomain.CurrentDomain.BaseDirectory
To get the current directory:
获取当前目录:
Environment.CurrentDirectory
Then you can concatenate your directory path (@"\Data\Names.txt")
然后你可以连接你的目录路径 ( @"\Data\Names.txt")
回答by Uthistran Selvaraj
If you need to get all the files in the folder named 'Data', just code it as below
如果您需要获取名为“Data”的文件夹中的所有文件,只需将其编码如下
string[] Documents = System.IO.Directory.GetFiles("../../Data/");
Now the 'Documents' consists of array of complete object name of two text files in the 'Data' folder 'Data'.
现在,“文档”由“数据”文件夹“数据”中两个文本文件的完整对象名称数组组成。
回答by Rickey
Copy Always to output directory is set then try the following:
设置始终复制到输出目录,然后尝试以下操作:
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
String Root = Directory.GetCurrentDirectory();
回答by Ziggler
I have a C# project (Windows Console Application). I have created a folder named Images inside project. There is one ico file called MyIcon.ico. I accessed MyIcon.ico inside Images folder like below.
我有一个 C# 项目(Windows 控制台应用程序)。我在项目中创建了一个名为 Images 的文件夹。有一个名为 MyIcon.ico 的 ico 文件。我访问了图像文件夹中的 MyIcon.ico,如下所示。
this.Icon = new Icon(@"../../Images/MyIcon.ico");
回答by Ris
Use this Code for read all files in folder and sub-folders also
使用此代码也可以读取文件夹和子文件夹中的所有文件
class Program
{
static void Main(string[] args)
{
getfiles get = new getfiles();
List<string> files = get.GetAllFiles(@"D:\Document");
foreach(string f in files)
{
Console.WriteLine(f);
}
Console.Read();
}
}
class getfiles
{
public List<string> GetAllFiles(string sDirt)
{
List<string> files = new List<string>();
try
{
foreach (string file in Directory.GetFiles(sDirt))
{
files.Add(file);
}
foreach (string fl in Directory.GetDirectories(sDirt))
{
files.AddRange(GetAllFiles(fl));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return files;
}
}
回答by Pranav Raikundalia
For Xamarin.iOS you can use the following code to get contents of the file if file exists.
对于 Xamarin.iOS,如果文件存在,您可以使用以下代码获取文件的内容。
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(documents, "xyz.json");
if (File.Exists(filename))
{
var text =System.IO.File.ReadAllText(filename);
}
回答by Ahmed Abdel Moneem
string myFile= File.ReadAllLines(Application.StartupPath.ToString() + @"..\..\..\Data\myTxtFile.txt")
回答by Josue Augusto Ramirez
This was helpful for me, if you use the
这对我很有帮助,如果你使用
var dir = Directory.GetCurrentDirectory()
the path fill be beyond the current folder, it will incluide this path \bin\debug What I recommend you, is that you can use the
路径填充超出当前文件夹,它会包括这个路径 \bin\debug 我建议你,是你可以使用
string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName
then print the dir value and verify the path is giving you
然后打印 dir 值并验证路径为您提供
回答by Satish Babu
Use this, it's working.
使用这个,它的工作。
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"TestData\test.json");
var response = JsonConvert.DeserializeObject<Response>(File.ReadAllText(path));
Please Note : You need to make sure that set the properties of file test.jsonas
请注意:您需要确保将文件的属性设置test.json为
"Build Action"="Content"
“构建操作”=“内容”
"Copy to Output Directory"="Copy always"
"复制到输出目录"="始终复制"

