WinUI 3 Desktop App - Directory.Exists/File.Exists not worked in folder AppData/Roaming

Andreas Mazatis 131 Reputation points
2022-03-06T16:38:20.517+00:00

I am trying to perform checks on a directory and file in the AppData/Roaming (C:\Users\MyUser\AppData\MyFolder\myFile.config) directory in a WinUI 3 Desktop Application. Neither the subdirectory "MyFolder" not the file "MyFile.config" exist, yet System.IO.Directory.Exists(roamingPath) and System.IO.File.Exists(filename) claim that the directory + file exist.

I have, although it should not matter, marked the functions anlog to a UWP app:
Internet (Client and Server)
Internet (Client)
Private Network (Client and Server)

If I try this on drive D (D:\Test\MyFolder\myFile.config), for example, it works as it should.

Does anyone know why this does not work in AppData Roaming?

Translated with www.DeepL.com/Translator (free version)

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
725 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
Windows Server Storage
Windows Server Storage
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Storage: The hardware and software system used to retain data for subsequent retrieval.
631 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 81,636 Reputation points
    2022-03-06T17:05:50.683+00:00

    This test works for me (Windows 10 21H1)

                // sAppdata = C:\Users\Christian\AppData\Roaming
                string sAppdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                // bExists = True
                bool bExists = System.IO.Directory.Exists(sAppdata);
                // sPath1 = C:\Users\Christian\AppData\Roaming\MyFolder
                string sPath1 = System.IO.Path.Combine(sAppdata, "MyFolder");
                // bExists1 = False
                bool bExists1 = System.IO.Directory.Exists(sPath1);
                // sPath2 = C:\Users\Christian\AppData\Roaming\MyFolder\myFile.config
                string sPath2 = System.IO.Path.Combine(sPath1, "myFile.config");
                // bExists2 = False
                bool bExists2 = System.IO.Directory.Exists(sPath2);
    
    0 comments No comments

  2. Thomas LCP 0 Reputation points
    2023-03-26T13:15:49.71+00:00

    Hi, same here. I'm on Windows 11 professional, version 10.0.22621.

    In a WinUi3 Desktop blank application based on .net 6.0,
    When I try to search if c:\Users\thoma\AppData\Local\StockaManagerScanner exist, it tell me yes and when I search for sms.config in this folder it tell me it exist too.

    I can understand that the folder should exist because I always run Directory.Create() for it. But I doesn't see it in windows file explorer and a console app base on .net framework doesn't find it

    For the sms.config file I don't understand because I never create it before checking if it exist. What break my mind is when I try to read the content of sms.config IT RETURN SOMETHING !!

    I tried you little test @Castorix31 and it give me the expected result (true, false, false).

    What did I miss here ? What can I do ?


  3. CyberSteve 156 Reputation points
    2023-04-14T17:49:15.4866667+00:00
    // Why not try something like the following:
    
    using Windows.Storage;
    
    async void GetFile()
    {
       try
       {
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
          StorageFile sampleFile = await localFolder.GetFileAsync("SomeFile.txt");
          String fileContents = await FileIO.ReadTextAsync(sampleFile);
       }
       catch (FileNotFoundException)
       {
          // file not found
       }
    
    
    
    
    0 comments No comments

  4. CyberSteve 156 Reputation points
    2023-04-15T14:06:44.12+00:00

    You can't just use your old .NET code with hardcoded paths and trying to get directories and expect it's going to work. If you're going to be doing Windows desktop coding you'll need to learn the classes and method's that return the locations you want to access. Don't worry about the actual directories / paths. Instead it's all about StorageFolder, StorageFile, KnownFolders, etc.

    0 comments No comments