question

MarkusForrer-1718 avatar image
0 Votes"
MarkusForrer-1718 asked DaisyTian-1203 commented

Application.LocalUserAppDataPath for .net 3.1 core wpf application

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.

windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MarkusForrer-1718
May I know if you have got any chance to check my answer? Does it work for you? Please let me know if it doesn't give you help.

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

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();


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarkusForrer-1718 avatar image
0 Votes"
MarkusForrer-1718 answered valdom commented

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";

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did some tests and apparently you have to concatenate them

And the Product is rather :

         System.Reflection.AssemblyProductAttribute productAttribute = (System.Reflection.AssemblyProductAttribute)System.Reflection.AssemblyProductAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(), typeof(System.Reflection.AssemblyProductAttribute));
         string sProductName = productAttribute.Product;

(read from [Properties) [Package] if I update it to test)

0 Votes 0 ·

Sorry I still do not understand. Does your code have to go into AssemblyInfo.cs?

0 Votes 0 ·

No, I just tested the values in a Button Click

0 Votes 0 ·
Show more comments
DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

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.



capture.png (11.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.