C# Cant deploy to Custom setting Path

33 Views Asked by At

I am developing a project in C# .net 8.0 in which I am using the application settings to save user settings:

enter image description here

Properties.Settings.Default.ScreenInfo = appLocation;
Properties.Settings.Default.Save();

The problem is that the following structure is generated when saving:

C:\Users\MyUserExample\AppData\Local\My_Test_Proyect_for_Doc\My_Test_Proyect_for_Doc_Url_sedth3gs1ffz55ydbe4p00hiuxb1wqm4\1.0.0.0

While other applications generate a name for their directories more simply, I intended to implement WSDD-Environment

Try this and then try to do it from the assemble event but it doesn't work for me ():

Try #1

using My_Test_Proyect_for_Docker.ClassDir;
using My_Test_Proyect_for_Docker.Forms;

namespace My_Test_Proyect_for_Docker
{
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
            CustomConfigPath();
            FlowApp();
        }

        private static void CustomConfigPath()
        {
        

            string customDirectoryName = "WSDD-Environment";

            string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            string customDirectoryPath = Path.Combine(localAppDataPath, customDirectoryName);

            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(customDirectoryPath, "app.config"));

        }
        
        private static vois FlowApp()
        {
            Properties.Settings.Default.ScreenInfo = appLocation;
            Properties.Settings.Default.Save();
        }
    }
}

Try #2

using My_Test_Proyect_for_Docker.ClassDir;
using My_Test_Proyect_for_Docker.Forms;

namespace My_Test_Proyect_for_Docker
{
    public partial class Main : Form
    {

        public Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            InitializeComponent();
            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            FlowApp();
        }

        private static Assembly? CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            
            string customDirectoryName = "WSDD-Environment";

            
            string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            
            string customDirectoryPath = Path.Combine(localAppDataPath, customDirectoryName);

            
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(customDirectoryPath, "user.config"));

            return null;
        }
        
        private static vois FlowApp()
        {
            Properties.Settings.Default.ScreenInfo = appLocation;
            Properties.Settings.Default.Save();
        }
    }
}

The directory is not created / it has no effect, the previous structure is still created...

Update #1:

I found this answer for VB.net from 2016, I don't know if it is correct to implement it in my scenario or how to do it, the code looks extensive and I don't know if in .net8.0 this has been changed or simplified.

How to change the predefined userconfig directory of my .NET application?

0

There are 0 best solutions below