C
C#3mo ago
hutoanhill

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
hutoanhill
hutoanhillOP3mo ago
(i am trying to store my custom settings file there, but it doesent update anymore...)
leowest
leowest3mo ago
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
hutoanhill
hutoanhillOP3mo ago
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
leowest
leowest3mo ago
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var appFolder = Path.Combine(appData, "MyApp");
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var appFolder = Path.Combine(appData, "MyApp");
hutoanhill
hutoanhillOP3mo ago
what does this do exactly? this is the filepath of the .exe?
jcotton42
jcotton423mo ago
It's AppData\Roaming\MyApp
leowest
leowest3mo ago
no, its a common OS folder for apps to save data to which is the indicated way to store those like cotton said
jcotton42
jcotton423mo ago
C:\Users\username\AppData\Roaming\MyApp that is.
leowest
leowest3mo ago
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
../../../../myappserttings.json
../../../../myappserttings.json
as the path, the amount of fallback "../" will depend on the type of project for example
\bin\Debug\net8.0-windows
\bin\Debug\net8.0-windows
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
public class MySetting
{
public string Name {get;set;}
public bool IsAutoSaveEnabled {get;set;}
public bool ShouldUseThis {get;set;}
public int CloseInSeconds {get;set;}
}
public class MySetting
{
public string Name {get;set;}
public bool IsAutoSaveEnabled {get;set;}
public bool ShouldUseThis {get;set;}
public int CloseInSeconds {get;set;}
}
and by serializing an instance of that u would get a well formatted json file with your settings in the appropriated directory
hutoanhill
hutoanhillOP3mo ago
okk. 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?
leowest
leowest3mo ago
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
hutoanhill
hutoanhillOP3mo ago
but if my program makes changes to the file i wont see those changes.
leowest
leowest3mo ago
u said it wont make changes
hutoanhill
hutoanhillOP3mo ago
they update when my program runs
...see them update when my program runs. ...
leowest
leowest3mo ago
and they wont change when i update my application.
hutoanhill
hutoanhillOP3mo ago
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
leowest
leowest3mo ago
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?
hutoanhill
hutoanhillOP3mo ago
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.
leowest
leowest3mo ago
im trying to understand what it is to better help u, I need some context
hutoanhill
hutoanhillOP3mo ago
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
leowest
leowest3mo ago
well in this scenario u would use secrets
hutoanhill
hutoanhillOP3mo ago
context: bane of all problem solving it seems
leowest
leowest3mo ago
u would not store anything sensitive in the app itself
hutoanhill
hutoanhillOP3mo ago
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
leowest
leowest3mo ago
hutoanhill
hutoanhillOP3mo ago
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.
leowest
leowest3mo ago
which is why u use secrets :Ok:
jcotton42
jcotton423mo ago
did you already push that secret to github? or rather, did you ever commit it?
hutoanhill
hutoanhillOP3mo ago
old ones, yes. but ive reset the auth token since then
jcotton42
jcotton423mo ago
good good but yeah, for a discord bot the $worker template is superb
leowest
leowest3mo ago
u see how it all goes we started with how to save db/settings files to secrets, context matters a lot for this things
jcotton42
jcotton423mo ago
huh, no tag?
MODiX
MODiX3mo ago
✅ Command successful. Added tag 'worker'.
jcotton42
jcotton423mo ago
$worker
MODiX
MODiX3mo ago
Worker Services - .NET
Learn how to implement a custom IHostedService and use existing implementations in C#. Discover various worker implementations, templates, and service patterns.
hutoanhill
hutoanhillOP3mo ago
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
leowest
leowest3mo ago
you could use azure vault its a bit more complex thou
jcotton42
jcotton423mo ago
key vault is supremely overkill unless they're already planning on deploying to azure
hutoanhill
hutoanhillOP3mo ago
yea this is the conclusion ive come to
jcotton42
jcotton423mo ago
like, you can use key vault from outside of azure, but it's a massive pain
hutoanhill
hutoanhillOP3mo ago
i realy only have one value that must stay a secret: the discord token
leowest
leowest3mo ago
but your only problem was that value leaking to a commit? if ur using environment vars it wont
hutoanhill
hutoanhillOP3mo ago
how do i keep the environment var when i deploy?
jcotton42
jcotton423mo ago
you would set it in your deployment environment what kinda place are you deploying the bot to?
hutoanhill
hutoanhillOP3mo ago
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...
leowest
leowest3mo ago
I will leave u to cotton, so it doesn't get confusing 🙂
jcotton42
jcotton423mo ago
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)
hutoanhill
hutoanhillOP3mo ago
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
jcotton42
jcotton423mo ago
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.
hutoanhill
hutoanhillOP3mo ago
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.
jcotton42
jcotton423mo ago
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.
hutoanhill
hutoanhillOP3mo ago
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?
jcotton42
jcotton423mo ago
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?
hutoanhill
hutoanhillOP3mo ago
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 builds
jcotton42
jcotton423mo ago
So, if you want to reference the folder your app is in, you should use
MODiX
MODiX3mo ago
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.
jcotton42
jcotton423mo ago
I'll be honest, I lost track of the original topic :kek:
hutoanhill
hutoanhillOP3mo ago
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.
jcotton42
jcotton423mo ago
So you want copy to output dir, then copy back? There's not really anything built in for that second step.
hutoanhill
hutoanhillOP3mo ago
exactly :( Guess i will go put in a sugestion for rider
leowest
leowest3mo ago
what u could do is define
#if DEBUG
write to X
#else
write to Y
#endif
#if DEBUG
write to X
#else
write to Y
#endif
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
Want results from more Discord servers?
Add your server