I added my program to the startup using RegistryKey like this :
private async void baslatKontrol_CheckedChanged(object sender, EventArgs e)
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string yol = Path.Combine(Environment.CurrentDirectory, "Asistanim.exe");
if (baslatKontrol.Checked)
{
rk.SetValue(yol, Application.ExecutablePath);
veri.Baslat = true;
await JsonVeriden(veri);
}
else
{
rk.DeleteValue(yol, false);
veri.Baslat = false;
await JsonVeriden(veri);
}
}
The problem is; There is a file called "Veri.json" which is located in my program's directory but every time i restart the computer and my program runs, it gives me the exception of "Veri.json is not found in C:\Windows\system32\Veri.json".
My program is running somewhere else and i want my program to use files that it creates on it's current directory. How can do so?
You need to use the application folder, or for example the startup exe path instead of environment current folder that is by default in windows or taken from the console command line point:
var path = Application.ExecutablePath;
To have a good consistency between running executable from IDE or from installed path, you can use:
/// <summary>
/// Indicate generated executable bin directory name.
/// </summary>
static public string BinDirectoryName
=> "Bin";
/// <summary>
/// Indicate generated executable bin\debug directory combination.
/// </summary>
static public string DebugDirectoryCombination
=> Path.Combine(BinDirectoryName, "Debug");
/// <summary>
/// Indicate generated executable bin\release directory combination.
/// </summary>
static public string ReleaseDirectoryCombination
=> Path.Combine(BinDirectoryName, "Release");
/// <summary>
/// Indicate the application executable file name.
/// </summary>
static public string ApplicationExeFileName
=> Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
/// <summary>
/// Indicate the root folder path of the application.
/// </summary>
static public string RootFolderPath
=> Directory.GetParent(Path.GetDirectoryName(Application.ExecutablePath
.Replace(DebugDirectoryCombination, BinDirectoryName)
.Replace(ReleaseDirectoryCombination, BinDirectoryName))).FullName;
Here we use standard initial project properties.
Now the most important is for the json file you need to use this path too to use it in your app, for example:
var pathJSON = Path.Combine(RootFolderPath, "Veri.json");
You can of course insert any subfolder...
If the json file is in the same directory as the executable, you can use these things to improve your application dichotomy.