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

前端 未结 7 781
走了就别回头了
走了就别回头了 2020-12-04 10:45

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

相关标签:
7条回答
  • 2020-12-04 11:07

    You can have it embedded (build action set to Resource) as well, this is how to retrieve it from there:

    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)

    0 讨论(0)
  • 2020-12-04 11:10

    From Solution Explorer, right click on myfile.txt and choose "Properties"

    From there, set the Build Action to content and Copy to Output Directory to either Copy always or Copy if newer

    enter image description here

    0 讨论(0)
  • 2020-12-04 11:14

    You have to use absolute path in this case. But if you set the CopyToOutputDirectory = CopyAlways, it will work as you are doing it.

    0 讨论(0)
  • 2020-12-04 11:17

    In this code you access to root directory project:

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

    then:

    StreamReader r = new StreamReader(_filePath + "/cities2.json"))
    
    0 讨论(0)
  • 2020-12-04 11:18
    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:

    _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:

    _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:

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

    Hope it helps.

    0 讨论(0)
  • 2020-12-04 11:28

    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
    
    0 讨论(0)
提交回复
热议问题