EDIT: How to access my project folder AFTER I have published my application? I want to access this file because I would like to edit it at runtime.
I want something like this “{ProjectDirectory}{ResourcesFolder}{myResource.txt}”
All of the above just point me waay waay down in the directories ‘projectbinx64debug’ so my ONLY solution is this
Is there not a simpler way to include resources for my application that can be editable? All I found about .resx files don’t fit my needs.
You have a couple of choices here.
If these are read-only assets like images, if you use “Copy to Output Directory” they should always be in a location relative to the app’s path. That means you really only need v2
or v3
above unless you’re writing a library, where it’s better to ask the caller to give you the path than try to figure it out yourself.
But another choice is your “app data” directory.
Environment.GetFolderPath() is how you find a lot of system-defined directories. It takes one of the Environment.SpecialFolder enum values to tell it which folder you want.
If you ask for SpecialFolder.ApplicationData
, you get the root of a directory that generally you should have read-write access to. It’s usually something like C:UsersAppData<username>Roaming
, but the point of using this method is you don’t have to hard-code that (it’s changed at least once in my career!). Generally you should create a directory here for your app, and use that for any read-write data you want your app to use. Generally this isn’t a spot you’re encouraged to tell a user to go look at and edit, but it works in a pinch.
For stuff the user is intended to save and edit, it’s recommended to use SpecialFolder.MyDocuments
as the root but let the user specify any directory.
The reason you’re having trouble is the output in the bin
folder isn’t really intended to be the final way an application is set up, but it is intended to be the BASIS. So if your resources are in parent directories of bin
, your project isn’t yet configured correctly.
You can fix that with options like “Copy to Output Directory”, or you can fix it by writing a build script that gathers the correct things from bin
and the correct resources, then deploys them to a final location. You shouldn’t write your program to think the final deployed state will look like your development machine’s state. Instead you should define what you want “deployed” to look like, write your code to work in that state, and make sure your final deployed state looks like what you expect.
Thanks for this.
Just to clarify: in almost all cases, you wouldn’t want to access folders in your project directory from code. C# is a compiled language. When you build your app and install in on a different machine, your project directoy is no longer there to access.
What you want, as others noted, is to copy that text file into your output directory, and that becomes part of your published app.
From what I gather it seems like you’re looking for something like app.config to read config values at runtime?
EDIT: I hadn’t opened the screenshot. If you want to read this jsonDB.txt file then, as othered mentioned, set its build option to Copy If Newer and it will be available in binDebug.. which you can already reach with your v2 or v3 vars
Yes this might be something like what I’m looking for, ty.
Set the resources to Copy to output dir. (might have to change build action also) Then all your resources will be copied at build time to the output dir.
Yes, how do I find this output dir?
Environment.CurrentDirectoy gives you the location of the executable. Set the file as content and to copy. When the application is compiled and deployed you won’t have your project structure right?
Apparently , Environment.CurrentDirectory points to some location in my %appdata% once it’s published. I want to be able to specify where my files are located, based on some fixed location eg ‘{project.exe location}{Files}{myFile.txt}’
This line is working fine for *debugging* purposes only. Once I publish my application, apparently it ‘CurrentDirectory’ translates some local ‘%AppData% location and there is no such file as ‘myfile.txt’ over there.
Can you just use a relative path and set the item’s to copy to output directory?
C# devs
null reference exceptions