Where can i put files so they will be in the same folder as the exe?
I've tried /bin, but this doesn't work.
61 Replies
(i am trying to store my custom settings file there, but it doesent update anymore...)
u put the file in the project folder
and then set them to copy if newer
the result is when u run or build or publish those files with be with the exe
alternatively u can make them resources which will embed the files in the assembly
and u can read / use them as reources
for a settings file
my recommendation is to use a common app folder
and when ur app first runs it creates the default settings there
ok. so is there i way i can tell it to copy the files back to the project folder when its done? My app has its own method for manipulating its settings, and i want to be able to watch that happen
what does this do exactly?
this is the filepath of the .exe?
It's AppData\Roaming\MyApp
no, its a common OS folder for apps to save data to
which is the indicated way to store those
like cotton said
C:\Users\username\AppData\Roaming\MyApp that is.
that is usually where u should be storing things like that
if for development purposes u want that file to be in your project list so u can easily modify it u can but I strongly advise u to use the above for the user.
U would need to do something like
as the path, the amount of fallback "../" will depend on the type of project
for example
that would be
../../../
to reach the project folder.
usually you shouldn't need to manually modify the settings file, u would do it via code and serialize it. Meaning the modifications should be done in code
not manually
and by serializing an instance of that u would get a well formatted json file with your settings in the appropriated directoryokk. let me explane a little more
Ive got an app i am developing. Its got some files, this settings file and a few others including my .db file, which it expects to find in its folder.
during development i need a to be able to see these files, manipulate them with other tools and see them update when my program runs. This used to be
/bin
i don't know how this changed, but the old versions of the default states of these files made it into this folder somehow.
when i deploy this app i am expecting these files to already be in the same place as the exe and they wont change when i update my application.
is ther any solution other than storeing these files in this mess of a folder? idealy in /bin?what I said initially
u put the files inside your solution explorer's project
then set it to copy if newer
it will be right next to the executable when u run, build or deploy
and u modify the files in the project not in the bin
but if my program makes changes to the file i wont see those changes.
u said it wont make changes
they update when my program runs
...see them update when my program runs. ...
and they wont change when i update my application.
meaning that i wont deploy new versions of these file when i push a new update
they store constants and sensitive information i don't want on my github and information about the current state of the application. if i pushed new version of these files when i updated the current state of the app would be lost
huh but u will delivery those direct to the user?
what kind of app are we talking about? asp.net?
desktop?
game?
and what kind of sensitive data? database connection etc?
if my program cant file these files they create empty versions of them, with no state data. But if i do some bug fixing and push an update, i don't want to overwrite these files.
im trying to understand what it is to better help u, I need some context
its a discord bot being hosting on a server i dont have a ton of access to, i will be allowed to push updates to this exe, but not much else
well in this scenario u would use secrets
context: bane of all problem solving it seems
u would not store anything sensitive in the app itself
discord auth token is the big one. i am also storing some other less sensitive stuff like my discord user id and the ids of a handfull of servers i want to mark as admin servers
Safe storage of app secrets in development in ASP.NET Core
Learn how to store and retrieve sensitive information during the development of an ASP.NET Core app.
yea, i started out hardcoding stuff like my discord token, but then you can see it on github... which is kinda bad
i will take a look at this.
which is why u use secrets
:Ok:
did you already push that secret to github?
or rather, did you ever commit it?
old ones, yes. but ive reset the auth token since then
good good
but yeah, for a discord bot the $worker template is superb
u see how it all goes we started with how to save db/settings files to secrets, context matters a lot for this things
huh, no tag?
✅ Command successful. Added tag 'worker'.
$worker
Worker Services - .NET
Learn how to implement a custom IHostedService and use existing implementations in C#. Discover various worker implementations, templates, and service patterns.
atm i am storing the auth token in riders environment vars but i don't think I've got a way to deply that. so i am currently just adding it to a config.json file and planning on dropping it in the same folder as the .exe
you could use azure vault
its a bit more complex thou
key vault is supremely overkill unless they're already planning on deploying to azure
yea this is the conclusion ive come to
like, you can use key vault from outside of azure, but it's a massive pain
i realy only have one value that must stay a secret: the discord token
but your only problem was that value leaking to a commit?
if ur using environment vars it wont
how do i keep the environment var when i deploy?
you would set it in your deployment environment
what kinda place are you deploying the bot to?
a frend of mine hosts a web server for personal stuff and offed it to me. I dont know a ton about it. This is the first time ive deployed somthing thats not just on my laptop...
I will leave u to cotton, so it doesn't get confusing 🙂
If you use the .net config system (which the worker sets up for you), you could pull it from env var or a settings json.
or you could make your existing setup read from a file if the env var isn't set
(or vice versa)
I've never done this. I am learning my professors did not teach us a lot about how this is normally done. I've created a data type for my config file along with JsonSerializerSettings rule for reading it and methods for loading in this config and saving it when i make changes.
This worked great until i realized it wasnt editing the file i thought it was anymore.
I've looked into appsettings.json which appears to be the standard for this, but it has this seemingly backward way of accessing it ive never used and dont understand and no one feels a need to explain it for some reason. on top of that there doesn't appear to be a way to modify it programmatically
Yeah, appsettings isn't made for you to write to it.
And the way you read from it is kinda weird, because the config system is made to aggregate many sources of configuration.
JSON, env vars, user secrets, Azure Key Vault, whatever.
i don't doubt it works fine for most people particularly when they know about it from the start and design with it in mind, but it also looks like it assumes the only way settings are muddied is by a user, which i am not doing.
Like i said i wont have regular access to the server this is being hosted on i am managing the the bots settings via discord.
It would then just be for static settings, like the bot's token.
Other, mutable config would be stored to separate JSON, or a database, or whatever.
Again, you don't have to use it, but it's quite useful.
yea. if i was just trying to store my token appsettings.json would be perfect
its the whole mutuality hook up. I've got the same issue with my conventual SQLite database, I have no idea where to put the .db file is. i need to be able to look at it was SQLiteStudio, and i need my program to be in the same folder with it so i can access it
the more i look around the more confused i get because it doesn't look like anyone ever though about this which is super strange because it seems so obvious.
like, I've got a /bin folder... shouldn't i be able to access stuff that's there?
Yeah, server apps are in a weird spot.
For desktop apps, you'd use AppData (or the OS' equivalent).
by
/bin
do you mean the folder the app was built/published to?my project, as rider built it, as a
/bin
folder. At some point my program was modifying files there because it currently has old versions of my config.json
file in it. ive been told this was suposed to behave like the place your app was when it buildsSo, if you want to reference the folder your app is in, you should use
❯ Property: System.AppContext.BaseDirectory
Gets the file path of the base directory that the assembly resolver uses to probe for assemblies.
React with ❌ to remove this embed.
I'll be honest, I lost track of the original topic :kek:
we are basicly still on it.
i need a place i can put/interact with files relitive to the location of the .exe so i can access them during development and my program can access them.
if i could snap my fingers i would say any files in /bin get copied to the location of the .exe when i run my project, then they get coppied back along with any files my program created.
So you want copy to output dir, then copy back?
There's not really anything built in for that second step.
exactly :( Guess i will go put in a sugestion for rider
what u could do is define
so when you're running on debug it would write to the file in the project folder and reading from it
and when u publish in release it will use the actual folder it ships with the exe
so copy if newer will always contain the right information for debug and release
althou u would need to be extra careful with these