Application.LocalUserAppDataPath for .net 3.1 core wpf application

Markus Forrer 1 Reputation point
2021-04-10T06:39:03.12+00:00

In a Windows Forms application I get the following path using Application.LocalUserAppDataPath:
C:\Users\<user>\AppData\Local\<company>\<app>\<version>

In my .net core 3.1 WPF application, I use Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

However, this only returns:
C:\Users\<user>\AppData\Local

I am missing the trailing part of the path:
\<company>\<app>\<version>

How do I get the full path as in a Windows Forms app?

Thanks for any hints.

Developer technologies Windows Presentation Foundation
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-04-10T10:06:07.49+00:00

    In WinForms, they are added to the Local AppData path from :
    Application.CompanyName, Application.ProductName, Application.ProductVersion

    So you can add them from the equivalent properties, like (tested with WPF .NET Core 5) =>

             System.Reflection.AssemblyCompanyAttribute companyAttribute = (System.Reflection.AssemblyCompanyAttribute)System.Reflection.AssemblyCompanyAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(), typeof(System.Reflection.AssemblyCompanyAttribute));
            string sCompanyName = companyAttribute.Company;
            string sProductName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
            string sProductVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
    
    0 comments No comments

  2. Markus Forrer 1 Reputation point
    2021-04-10T11:51:06.627+00:00

    Thank you for your answer.

    Do I have to add the properties to AssemblyInfo.cs? Or do I have to add them like:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\<CompanyName>\<ProductName>\"<ProductVersion";


  3. DaisyTian-1203 11,646 Reputation points
    2021-04-13T07:53:32.18+00:00

    You can use Application.LocalUserAppDataPath for .net 3.1 core wpf application by below steps:

    Step 1: Add <UseWindowsForms>true</UseWindowsForms> in PropertyGroup for project's csproj file
    Step 2: Right click project's Dependenices >Choose Add COM Reference...> Select System_Windows_Forms and click OK , below is my .csproj code

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  
      
      <PropertyGroup>  
        <OutputType>WinExe</OutputType>  
        <TargetFramework>netcoreapp3.1</TargetFramework>  
        <UseWindowsForms>true</UseWindowsForms>  
        <UseWPF>true</UseWPF>  
        <Company>MyCompanyH</Company>  
        <Authors>Daisy</Authors>  
        <PackageId>MyProID</PackageId>  
      </PropertyGroup>  
      
      <ItemGroup>  
        <COMReference Include="{215d64d2-031c-33c7-96e3-61794cd1ee61}">  
          <WrapperTool>tlbimp</WrapperTool>  
          <VersionMinor>4</VersionMinor>  
          <VersionMajor>2</VersionMajor>  
          <Guid>215d64d2-031c-33c7-96e3-61794cd1ee61</Guid>  
        </COMReference>  
      </ItemGroup>  
      
    </Project>  
    

    Then you can use it in project like :

    private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                string str = System.Windows.Forms.Application.UserAppDataPath;  
                MessageBox.Show(str);  
            }  
    

    The MessageBox result is:
    87278-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.