简体   繁体   English

从项目中存在的文件夹中读取文件

[英]Read files from a Folder present in project

I have a C# project (Windows Console Application).我有一个 C# 项目(Windows 控制台应用程序)。 I have created a folder named Data inside project.我在项目中创建了一个名为Data的文件夹。 There are two text files inside folder Data. Data 文件夹中有两个文本文件。

How can I read the text files from "Data" folder.如何从“数据”文件夹中读取文本文件。 I tried below things.我试过下面的东西。

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!谢谢!

below code should work:下面的代码应该工作:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\Names.txt");
string[] files = File.ReadAllLines(path);

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"

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'.现在“文档”由“数据”文件夹“数据”中两个文本文件的完整对象名称数组组成。

I have a C# project (Windows Console Application).我有一个 C# 项目(Windows 控制台应用程序)。 I have created a folder named Images inside project.我在项目中创建了一个名为 Images 的文件夹。 There is one ico file called MyIcon.ico.有一个名为 MyIcon.ico 的 ico 文件。 I accessed MyIcon.ico inside Images folder like below.我访问了 Images 文件夹中的 MyIcon.ico,如下所示。

this.Icon = new Icon(@"../../Images/MyIcon.ico");

Copy Always to output directory is set then try the following: Copy Always to output directory 已设置,然后尝试以下操作:

 Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
 String Root = Directory.GetCurrentDirectory();

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);
}

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;
    }
}
string myFile= File.ReadAllLines(Application.StartupPath.ToString() + @"..\..\..\Data\myTxtFile.txt")

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然后打印目录值并验证路径是否为您提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM