Advertisement

[C# Console] - Working with external data files....

Started by December 28, 2018 01:24 AM
7 comments, last by timothyjlaird 5 years, 11 months ago

G'Day....

I am having a little trouble with my current project I can not seem to google an answer for. Basically I am using external text files as I want to be able to easily edit them after getting the framework of my console application up and running. I do not want to do anything simple at the moment, though I may write a simple phaser at a latter stage.

I have a test code like this....


        public static string ReadDataFile()
        {
            string text = File.ReadAllText(@"_Debug\testData.txt");
            return text;
        }

cRjiawf.png

The problem is when I go to test by pressing the F5 Key it is looking the the relative path of the Bin/Debug Folder in the solution.... Now I could move all the data files into that folder (and this it works) but I am sure that is not the correct way to do it.

TL;DR - How do I use paths to find data files with Fie.ReadAllText and then be able to compile a test run using F5 or a Release Build and have those DataFiles and directory structure copied to the location the binary is made so the application can run correctly. Or if I am way off track, any solution please.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

I generally just define where I expect things to be in the program I'm writing(create some sort of "path" string variable to hold it), place the files there if they need to be accessed during dev and do my development.  If I need to have a specific directory structure/etc for the end user I'll usually rely on the installer application to handle this, or create the desired directories and copy/create the files programmatically on first execution.

Advertisement

Several options.

One is to change the working directory when you debug, so the file will be in the right place relative to your path location. In the configuration properties, choose the Debugging panel, then change the working directory from the default of $(ProjectDir) to $(TargetDir).  Now instead of running from the solution's directory, it will be launched from the same directory as the executable

Another option is to add a command line argument for the configuration files. It is more useful for a general purpose so anyone can run it with their preferred location.Specify the full path as a command line parameter, and then open the file from there.

You could also use configuration files that specify the files to use.  C# will automatically load configuration files, they're XML format, and you can configure the application to use whatever you want.  If the file name were specified in the application settings as "datafile", you can read the string with ConfigurationManager.AppSettings.Get("datafile");  Then you can have different configurations for each executable.

Those are the first three that come to mind. I'm sure you can find more options if you're feeling creative.

thanks guys.... I have hacked a way around this..... I'll check out frob's options, but I will keep my hack for now...

As I wanted a "single edit" solution with no copies of the data files, so at any time I can edit them and I do not have to worry about anything... I chose to use a "symbolic link".

What this dose is create a special kind of folder in the bin/debug that when you cd into or click on or w/e it actually takes you to another folder on the hard drive....

Quote

mklink /D "c:\xx\xx\xx" "d:\yy\yy\yy"


mklink /D "source directory" "new symbolic link direcotry"

This way the thing looks into the directory in bin/debug but is actually accessing the files in my project directory. Seems to work fine. (you'd need to copy the files for release if you sent it to someone)

I'll look into frob's response latter.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

Usually what I do in Visual Studio for simple data files in hobby projects (and more importantly, unit tests):

  • Add the data file to the project (like in your screenshot).
  • Right click on the file in the solution explorer and select 'Properties'.
  • In the properties panel, change "Copy to Output Directory" to "Copy Always".

This just copies the file into the output folder structure (where the EXE or DLL gets created).

1 hour ago, Nypyren said:

Usually what I do in Visual Studio for simple data files in hobby projects (and more importantly, unit tests):

  • Add the data file to the project (like in your screenshot).
  • Right click on the file in the solution explorer and select 'Properties'.
  • In the properties panel, change "Copy to Output Directory" to "Copy Always".

This just copies the file into the output folder structure (where the EXE or DLL gets created).

dose it copy it into the root with the exe or dose it keep the folder structure?

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
Advertisement

It keeps the folder structure.  For example if you make a folder in the solution called "Data" and put a text file in it, then set the text file to "Copy Always", your loading code would need to use "Data\TextFile1.txt" for the path.

You can also hand-edit the .csproj file to include all files in a folder:


  <ItemGroup>
    <Content Include="Data\*.txt">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

 

As said there are so many solutions to the problem, one that seems to work well:

Stick one text file that will go wherever your BIN folder lives. This file can have key value pairs that point to directories where your resources (text files, images, etc) are at.

Your program simply looks for that file when it loads in it's current directory, tries to find resources where the file says they are and goes about it's business.

That approach has the benefit of changing where the resources go without changing code and it's very transparent how the mechanism works. Also it lends itself well to multiple resource paths for when you want to load more than text.

This topic is closed to new replies.

Advertisement