【问题标题】:How to read a text file in project's root directory?如何读取项目根目录中的文本文件?
【发布时间】:2011-06-20 19:44:27
【问题描述】:

我想读取添加到项目根目录的文本文件的第一行。这意味着,我的解决方案资源管理器在我的项目中显示 .txt 文件以及我的 .cs 文件。

所以,我尝试这样做:

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

但这不起作用,因为它指的是 Bin 文件夹,而我的文件不在其中……我怎样才能使它起作用? :/

谢谢

【问题讨论】:

  • 顺便说一句,StreamReader 的名称有点混乱,在我看来微软应该命名为 TextStreamReader

标签: c# wpf


【解决方案1】:

在解决方案资源管理器中,右键单击 myfile.txt 并选择“属性”

从那里,将Build Action 设置为contentCopy to Output DirectoryCopy alwaysCopy if newer

【讨论】:

  • 我不想让这个文件暴露给我的用户...有没有办法将它嵌入到 dll 中并从那里读取?
  • @foreyez 将字符串作为资源添加到您的项目中,而不是使用文件。请注意,数据不会无法获取 - 此文件包含的数据有多敏感?
  • 将构建选项设置为Embedded Resource 并阅读有关如何读取嵌入式资源blogs.msdn.com/b/nikola/archive/2008/05/14/… 的文章
  • 如果我为项目创建设置,那么我无法使用属性。有没有其他解决方案?
【解决方案2】:

您可以通过以下方式获取网站项目的根目录:

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

或者你可以像这样获取基本目录:

AppDomain.CurrentDomain.BaseDirectory

【讨论】:

    【解决方案3】:

    将资源文件添加到您的项目(右键单击项目->属性->资源)。在它说“字符串”的地方,您可以切换为“文件”。选择“添加资源”并选择您的文件。

    您现在可以通过 Properties.Resources 集合引用您的文件。

    【讨论】:

    • 当我尝试这个时,我得到“路径中的非法字符”。资源似乎将您的文件内容保存为字符串,并在一定长度后将其截断。
    【解决方案4】:
    private string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
    

    上面的方法会给你带来这样的结果:

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

    从这里您可以使用 System.IO.Directory.GetParent 向后导航:

    _filePath = Directory.GetParent(_filePath).FullName;
    

    1 次将带您到 \bin,2 次将带您到 \myProjectNamespace,所以它会是这样的:

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

    好吧,现在你有了“C:\Users\myuser\Documents\Visual Studio 2015\Projects\myProjectNamespace”之类的东西,所以只需将最终路径附加到您的文件名,例如:

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

    希望对你有帮助。

    【讨论】:

    • 谢谢,但是可以 _filePath = Path.Combine(_filePath, "myfile.txt"); // 时尚 :)
    【解决方案5】:

    您也可以将其嵌入(构建操作设置为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);
        }
    }
    

    (一些代码取自this answerCharles

    【讨论】:

      【解决方案6】:

      在这种情况下,您必须使用绝对路径。但是如果你设置了CopyToOutputDirectory = CopyAlways,它就会像你那样工作。

      【讨论】:

        【解决方案7】:

        在这段代码中你访问root目录项目:

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

        然后:

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

        【讨论】:

          猜你喜欢
          • 2019-11-05
          • 2015-07-10
          • 1970-01-01
          • 2021-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多