1

I would like to modify the contents of a file placed in my project folder (Specifically "Assets/recentChanges.txt") at runtime. Let's say it contains information about recent changes in application. It is supposed to be read and bound to a textbox control, which later on can be edited by developers and saved back. The problem with content files is that they are copied to output directories and therefore I'm not editing the original file in Assets folder, but the one in bin/Debug/Assets/ folder. And the Embedded Resource build action is not an option since such files cannot be modified.

The question is: how can I modify mentioned .txt file in its original root/Assets/recentChanges.txt location during runtime? All of the below instructions point to similiar build-dependant path:

Environment.CurrentDirectory;
Assembly.GetExecutingAssembly().Location;

Or, in other words, what would be the correct approach for implementing "Recent Changes" section with following conditions:

  • based on text file in its original project location
  • possibilty to modify the file at runtime through UI
  • file copied to the output folder and visible to the user (through textbox) in published application

I am aware that it's probably not the perfect approach, because "recent changes" info could be collected from database or the .txt file could be modified manually and then copied to output locations... but nevertheless I think the issue is somewhat confusing and I would love some help. Thanks!

  • 2
    The compiled and running application doesn't know anything about some "original" location. This location is only important to Visual Studio when the application is built. If you want to modify the file in this folder you should specify an absolute path to it, i.e. "c:\your_project_folder\file.txt". The .exe cannot be supposed to know from where it was compiled. – mm8 Feb 10 '17 at 13:09
  • Honestly if the file is just a text file and not a WPF media resource like PNG, why deploy it like a "resources" file? _"I would like to modify the contents of a file ...at **runtime**...It is supposed to be read and bound to a textbox control, which later on can be **edited** by **developers** and **saved back**"_ - Additionally, why the gollies are developers changing a SDLC file for a program they created **in the program they created**?? Bet that won't show up in say Git. Please use SDLC property! –  Feb 10 '17 at 13:21
  • I'm voting to close this question as off-topic because OP is attempting to do something against the natural order of SDLC –  Feb 10 '17 at 13:21
  • And what prey-tell happens when you run this on another machine? Currently? By multiple developers? Where the original path may or may not exist. I don't think you've thought this through –  Feb 10 '17 at 13:34
  • @mm8 I will have to go with something like that than. Combinging `Environment.CurrentDirectory + @"\..\..\Assets\recentChanges.txt";` those two to go back to root folder will work for me. Thanks! – J. Piaskowski Feb 10 '17 at 13:37
  • So your issue/question is solved then? Should I post my comment as an answer? – mm8 Feb 10 '17 at 13:37
  • @MickyD I can see my mistake and wrong approach, but the desire to modify normal text files used in project at runtime just didn't seem that weird to me. Regarding path - I do check if it exists first and this is meant to be used by 2 developers maximum. Although I understand your concern. Thanks. – J. Piaskowski Feb 10 '17 at 13:39
  • 1
    @mm8 Yes. I guess the question wasn't right in the first place, but I followed your suggestion. – J. Piaskowski Feb 10 '17 at 13:41

1 Answers1

0

The compiled and running application doesn't know anything about some "original" location. This location is only important to Visual Studio when the application is built.

If you want to modify the file in this folder you should specify an absolute path to it, i.e. "c:\your_project_folder\file.txt".

The .exe cannot be supposed to know from where it was compiled

mm8
  • 163,881
  • 10
  • 57
  • 88