2

I have windows form application and i have some text files in folder as in image. How can i reach them and read that files?

appimage

I also tried code below but get error value cannot be null. parameter name stream

Assembly asm = Assembly.GetExecutingAssembly();
StreamReader reader = new StreamReader(asm.GetManifestResourceStream("NexoClientApp.JsonRequests.Login.txt"));
string jtext = reader.ReadToEnd();

Thanks

saulyasar
  • 797
  • 1
  • 17
  • 45
  • 2
    do you even include your files _as embedded resource_? – Franz Gleichmann Jul 14 '20 at 10:20
  • 2
    Right click the txt file in VS and check the property to Copy which will move the latest version of file into the project bin folder. Then you can access by simply open the file without by name in cs without a path. The exe by default expects any file to be in same folder and the cs executable. – jdweng Jul 14 '20 at 10:20
  • 2
    Set the `copy to output` setting of those files to `If Newer` or `Always` and use a relative path, eg `File.ReadAllText("JsonRequests\\Login.txt")`. The default working directory of an application is the app's folder itself, so you *don't* need to use `GetExecutingAssembly`. `asm.GetManifestResourceStream` is meant to load embedded resources only – Panagiotis Kanavos Jul 14 '20 at 10:22
  • @PanagiotisKanavos thanks man this way is ok you can add as an answer – saulyasar Jul 14 '20 at 10:29

3 Answers3

1

be sure to copy the files while building! See the properties of your text files like in this example: (copy if newer will also work fine)

VS build options

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
0

If you want the files to be installed as files in the destination system by keeping the project structure, you need to read them as disk items, and create a setup or simple zip that copy these files to restore the project structure for needed items.

To read them, in case of the executable is generated in bin folder as by default for every VS project:

var lines = File.ReadAllLines(RootFolderPath + @"JsonRequests\Login.txt");

public string RootFolderPath
  = Directory.GetParent
    (
      Path.GetDirectoryName(Application.ExecutablePath.ToLower()
                            .Replace("\\bin\\debug\\", "\\bin\\")
                            .Replace("\\bin\\release\\", "\\bin\\"))
    ).FullName
  + Path.DirectorySeparatorChar;

We remove the debug or release folder to get the project/app root path to be able to read the desired file.

If the binary is generated in another folder, use it. If in the root itself, use it as-is. If you change to have the save folder for release and debug, adapt that.

In WinForms, all methods to get the executable path returns a path having by default debug or release... but here we need the root path of the project or installed app.

Here we don't set copy files to the executable folder in the solution explorer, but we keep the project structure on disk.

You can also create a ressource file to embbed the files you want and use GetManifestResourceStream or use the @FalcoAlexander answer to copy files in the executable folder and read from there.

  • What is `.Replace("\\bin\\debug\\", "\\bin\\").Replace("\\bin\\release\\", "\\bin\\"))` supposed to do? If files are copied to the output directory, they will be in `bin\Debug` if the configuration is `Debug` (assuming a default configuration). – CodeCaster Jul 14 '20 at 10:34
  • https://stackoverflow.com/questions/362790/what-is-the-best-way-to-determine-application-root-directory – Pavel Anikhouski Jul 14 '20 at 10:57
0
string dir = AppDomain.CurrentDomain.BaseDirectory;

//OR

string dir = Application.StartupPath;

string login = File.ReadAllText(dir+"/JsonRequests/Login.txt"); //read some file

//OR

List<FileInfo> files = new DirectoryInfo(dir).GetFiles().ToList(); //get all info about files in root dir