0

I would like to get the AppData path of my current application. I used to do this with Application.UserAppDataPath but it's in the System.Windows.Forms namespace and I want to get rid of that. I need it in the repository layer and its a wpf application anyway.

No more reason than to reduce the smell.

Thy and BR, Matthias

Matthias
  • 1,267
  • 1
  • 15
  • 27
  • See [here](http://stackoverflow.com/questions/938421/getting-the-applications-directory-from-a-wpf-application) and [here](http://stackoverflow.com/questions/3123870/find-the-location-of-my-applications-executable-in-wpf-c-or-vb-net) or [here](http://stackoverflow.com/questions/9659127/folder-to-store-data-files-locally-in-wpf-application)? – TaW Nov 25 '16 at 18:56
  • 1
    thats not what i want. i want my applications specific appdata folder. not just the appdata folder of the user. and i don't want to build the appdata-folder-path by hand, starting with the appdata-folder. look how Application.UserAppDataPath works. it builds a path like "Base Path\CompanyName\ProductName\ProductVersion" The reason is: I want to save some user specific files from code. – Matthias Nov 25 '16 at 19:09
  • Are you looking for [Application.LocalUserAppDataPath property](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.localuserappdatapath(v=vs.110).aspx) This seems to do the trick and return `BasePath\CompanyName\ProductName\ProductVersion` **edit**: gotcha - without Forms-Namespace ... - I'll have another look intot that matter – Pilgerstorfer Franz Nov 30 '16 at 12:18
  • 1
    http://stackoverflow.com/questions/915210/how-can-i-get-the-path-of-the-current-users-application-data-folder, http://stackoverflow.com/questions/3091606/accessing-application-data-folder-path-for-all-windows-users, tons of duplicates. You didn't look very hard. – Cody Gray - on strike Nov 30 '16 at 13:42
  • 3
    that's unbelievable. i pointed out like three times that the simple appdata folder is NOT WHAT I WANT! if i just would have known that this simple questions generates that much troll posts and mean downvotes i would have never asked in the first place. – Matthias Nov 30 '16 at 18:53
  • Hmm, so...your problem is actually that you didn't know how to combine paths? You should have said that. `System.IO.Path.Combine` is what you're looking for. That will concatenate strings to form a path. Perhaps instead of freaking out and calling everyone trolls, you should consider why so many people misinterpreted your question. It was not as clear as it should have been. You made it quite clear that you wanted to avoid `System.Windows.Forms`, but nowhere does it say that you did not want the AppData folder. In fact, you implied that you *did*! – Cody Gray - on strike Dec 01 '16 at 15:00
  • You also seem (from comments to other answers) to be operating under the misconception that all applications have their own specific/unique folder under the Application Data directory. This is not the case. In fact, it is *never* the case, unless you have explicitly created that directory. You can create directories using `Directory.CreateDirectory`, or via a variety of other methods. – Cody Gray - on strike Dec 01 '16 at 15:02
  • 1
    I didnt mean to make you feel bitter. However, please spare me. I wrote "the AppData path of my current application" in the first sentence and even linked the library entry of the UserAppDataPath property I use atm. Yes, i do work under the circumstances that my application has a specific folder within appdata since I store some data in the user-scoped settings. however as I pointed out, I want the functionality of the UserAppDataPath property but not the smelly namespace. As you can see in the linked doku, the property will create the folder when it does not exist. – Matthias Dec 01 '16 at 18:54
  • 1
    It is not about duplicating the code of Application.UserAppDataPath, its about clean code and getting rid of the smell of a Windows.Forms namespace in the data layer of a WPF application. however, I will of course accept that smell before duplicating perfectly working code just for the sake of a cleaner namespace using. I was just hoping a similar method exists in an other, more general, namespace and I could not find it. That's not the case, apparently. Therefore I created a feature request (see comment on answer) and will go with the Forms namespace for now. – Matthias Dec 01 '16 at 18:59

2 Answers2

2

I just had a look into the source coude of System.Windows.Forms.dll. There you can find this:

Disassembly of Application.LocaluserAppDataPath

When starting to clone this code in a class on my own I failed after having several properties, other assemblies, reflection, helper aso. So I wrote a VERY SIMPLIFIED class on my own.

    public static string LocalUserAppDataPath
    {
        get
        {
            return GetDataPath(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        }
    }

    private static string GetDataPath(string basePath)
    {
        string format = @"{0}\{1}\{2}\{3}";
        string companyName = "YOUR_COMPANYNAME";
        string productName = "PRODUCTNAME";
        string productVersion = "PRODUCTVERSION";
        object[] args = new object[] { basePath, companyName, productName, productVersion };
        string path = string.Format(CultureInfo.CurrentCulture, format, args);

        return path;
    }

I know that there are some hardcoded strings. But right now, refering to your given limitations, I would give this code a try.

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • 1
    Thx man. At least one person understood what i was looking for. I mark your post as answer due to the effort. however I think I'm not gonna duplicate the code but rather go with the strange namespace. I think its odd that this method is within the Forms namespace. – Matthias Nov 30 '16 at 18:57
  • 1
    i created a suggestion to move this method to another namespace: https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/17288504-move-userappdatapath-to-another-namespace-than-for – Matthias Nov 30 '16 at 19:16
0

Maybe you can try it with SpecialFolder?

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

or

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
nicoh
  • 374
  • 2
  • 12
  • 2
    NOT WHAT I WANT. Thats the appdata folder. i want my applications specific path within the appdata folder. i really start regretting that i asked the question in the first place. thx for the effort anyway. – Matthias Nov 30 '16 at 18:55