Search code examples
c#.netxamlmauicreate-directory

SpecialFolder.ApplicationData not working in .net MAUI?


I cannot get my directories made to AppData/Roaming while using this code in .net MAUI:

        RootLocation = new DirectoryInfo(GetFolderPath(SpecialFolder.ApplicationData) + "/MyApp");
        SaveLocation = new DirectoryInfo(RootLocation.FullName + "/saves");
        SaveLocation.Create();
        BackupLocation = new DirectoryInfo(RootLocation.FullName + "/backups");
        BackupLocation.Create();

However if I change SpecialFolder.ApplicationData to SpecialFolder.Desktop it does work on Desktop. It seems to be a permissions issue however in Visual Studio 2019 using WPF I can still save to AppData/Roaming. Could this be an issue with Visual Studio 2022 Preview?


Solution

  • if (IsWindows())
    {
        savingPath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
        savingPath = Path.Combine(savingPath, "yourfolder");
        if (!Directory.Exists(savingPath))
        {
            Directory.CreateDirectory(savingPath);
        }
    }
    

    I think it will be better. And the code is running correctly on my desktop and my Android app