简体   繁体   English

如何读取项目根目录中的文本文件?

[英]How to read a text file in project's root directory?

I want to read the first line of a text file that I added to the root directory of my project.我想读取添加到项目根目录中的文本文件的第一行。 Meaning, my solution explorer is showing the .txt file along side my .cs files in my project.意思是,我的解决方案资源管理器在我的项目中与我的 .cs 文件一起显示 .txt 文件。

So, I tried to do:所以,我试图这样做:

TextReader tr = new StreamReader(@"myfile.txt");
string myText = tr.ReadLine();

But this doesn't work since it's referring to the Bin Folder and my file isn't in there... How can I make this work?但这不起作用,因为它指的是 Bin 文件夹而我的文件不在那里......我怎样才能做到这一点? :/ :/

Thanks谢谢

From Solution Explorer, right click on myfile.txt and choose "Properties"在解决方案资源管理器中,右键单击 myfile.txt 并选择“属性”

From there, set the Build Action to content and Copy to Output Directory to either Copy always or Copy if newer从那里,将Build Action设置为content并将Copy to Output Directory设置为Copy always Copy if newerCopy if newer

在此处输入图片说明

You can use the following to get the root directory of a website project:您可以使用以下命令获取网站项目的根目录:

String FilePath;
FilePath = Server.MapPath("/MyWebSite");

Or you can get the base directory like so:或者您可以像这样获取基本目录:

AppDomain.CurrentDomain.BaseDirectory

Add a Resource File to your project (Right Click Project->Properties->Resources).将资源文件添加到您的项目(右键单击 Project->Properties->Resources)。 Where it says "strings", you can switch to be "files".在它说“字符串”的地方,您可以切换为“文件”。 Choose "Add Resource" and select your file.选择“添加资源”并选择您的文件。

You can now reference your file through the Properties.Resources collection.您现在可以通过Properties.Resources集合引用您的文件。

private string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);

The method above will bring you something like this:上面的方法会给你带来这样的东西:

"C:\Users\myuser\Documents\Visual Studio 2015\Projects\myProjectNamespace\bin\Debug"

From here you can navigate backwards using System.IO.Directory.GetParent:从这里您可以使用 System.IO.Directory.GetParent 向后导航:

_filePath = Directory.GetParent(_filePath).FullName;

1 time will get you to \\bin, 2 times will get you to \\myProjectNamespace, so it would be like this: 1 次会让你到 \\bin,2 次会让你到 \\myProjectNamespace,所以它会是这样的:

_filePath = Directory.GetParent(Directory.GetParent(_filePath).FullName).FullName;

Well, now you have something like "C:\\Users\\myuser\\Documents\\Visual Studio 2015\\Projects\\myProjectNamespace", so just attach the final path to your fileName, for example:好吧,现在您有类似“C:\\Users\\myuser\\Documents\\Visual Studio 2015\\Projects\\myProjectNamespace”的内容,因此只需将最终路径附加到您的文件名,例如:

_filePath += @"\myfile.txt";
TextReader tr = new StreamReader(_filePath);

Hope it helps.希望能帮助到你。

You can have it embedded (build action set to Resource ) as well, this is how to retrieve it from there:您也可以将其嵌入(构建操作设置为Resource ),这是从那里检索它的方法:

private static UnmanagedMemoryStream GetResourceStream(string resName)
{
    var assembly = Assembly.GetExecutingAssembly();
    var strResources = assembly.GetName().Name + ".g.resources";
    var rStream = assembly.GetManifestResourceStream(strResources);
    var resourceReader = new ResourceReader(rStream);
    var items = resourceReader.OfType<DictionaryEntry>();
    var stream = items.First(x => (x.Key as string) == resName.ToLower()).Value;
    return (UnmanagedMemoryStream)stream;
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
    string resName = "Test.txt";
    var file = GetResourceStream(resName);
    using (var reader = new StreamReader(file))
    {
        var line = reader.ReadLine();
        MessageBox.Show(line);
    }
}

(Some code taken from this answer by Charles ) 查尔斯这个答案中提取的一些代码)

You have to use absolute path in this case.在这种情况下,您必须使用绝对路径。 But if you set the CopyToOutputDirectory = CopyAlways , it will work as you are doing it.但是,如果您设置CopyToOutputDirectory = CopyAlways ,它将像您一样工作。

In this code you access to root directory project:在此代码中,您可以访问root目录项目:

 string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);

then:然后:

StreamReader r = new StreamReader(_filePath + "/cities2.json"))

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

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