Read file that is in solution in C #

1

I created a file in the solution of my project. How do I read this file? It should stay in solution because when compiling it should be wrapped in .exe.

    
asked by anonymous 01.06.2016 / 19:58

1 answer

2

First you need to configure the file to be copied to bin in build of your solution :

  • Right click on file > Properties (or select the file and press F4 ;
  • Under Build Action , select Content ;
  • Under Copy to Output Directory , select either Copy always or Copy if newer .
  • If it is a text file, you can read this file with the following command:

    var reader = new StreamReader(@"meuarquivo.txt");
    

    For files within the executable, the translation of this article is essentially: link . I'll explain succinctly.

  • Right click on file > Properties (or select the file and press F4 ;
  • Under Build Action , select Embedded Resource ;
  • Accessing

    using System.IO;
    using System.Reflection;
    
    try
    {
        var assembly = Assembly.GetExecutingAssembly();
        var imageStream = assembly.GetManifestResourceStream("MeuArquivo.txt");
    }
    catch
    {
        throw new Exception("Erro acessando arquivo de resource.");
    }
    
        
    01.06.2016 / 20:06