C
C#4w ago
hutonahill

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
hutonahill
hutonahill4w ago
(i am trying to store my custom settings file there, but it doesent update anymore...)
leowest
leowest4w 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
hutonahill
hutonahill4w 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
leowest4w 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");
hutonahill
hutonahill4w ago
what does this do exactly? this is the filepath of the .exe?
jcotton42
jcotton424w ago
It's AppData\Roaming\MyApp
leowest
leowest4w 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
jcotton424w ago
C:\Users\username\AppData\Roaming\MyApp that is.
leowest
leowest4w 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
hutonahill
hutonahill4w 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
leowest4w 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
hutonahill
hutonahill4w ago
but if my program makes changes to the file i wont see those changes.
leowest
leowest4w ago
u said it wont make changes
hutonahill
hutonahill4w ago
they update when my program runs
...see them update when my program runs. ...
leowest
leowest4w ago
and they wont change when i update my application.
Want results from more Discord servers?
Add your server