summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/WinRTFolderPaths.cs
blob: ae065f03541fff361518ef5abfcaad17d2f53768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.Foundation.Metadata;
using Windows.Storage;
using System.IO;
using static System.Environment;

namespace System
{
    internal static class WinRTFolderPaths
    {
        public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
        {
            // For testing we'll fall back if the needed APIs aren't present.
            //
            // We're not honoring the special folder options (noverify/create) for a few reasons. One, most of the
            // folders always exist (e.g. it is moot). Two, most locations are inaccessible from an appcontainer
            // currently - making it impossible to answer the question of existence or create if necessary. Thirdly,
            // the Win32 API would create these folders with very specific ACLs, which even in the cases we can create
            // are a significant compat risk (trying to replicate internal Windows behavior- it is documented that they
            // set specific ACLs, but not which ones).
            if (ApiInformation.IsTypePresent("Windows.Storage.UserDataPaths"))
            {
                return GetFolderPathCoreCurrent(folder);
            }
            else
            {
                return GetFolderPathCoreFallBack(folder);
            }
        }

        private static string GetFolderPathCoreCurrent(SpecialFolder folder)
        {
            // While all of these give back real paths, most of them are not accessible
            // from an appcontainer currently (they will give access denied)
            switch (folder)
            {
                case SpecialFolder.ApplicationData:
                    return UserDataPaths.GetDefault().RoamingAppData;
                case SpecialFolder.CommonApplicationData:
                    return AppDataPaths.GetDefault().ProgramData;
                case SpecialFolder.LocalApplicationData:
                    return AppDataPaths.GetDefault().LocalAppData;
                case SpecialFolder.Cookies:
                    return AppDataPaths.GetDefault().Cookies;
                case SpecialFolder.Desktop:
                    return AppDataPaths.GetDefault().Desktop;
                case SpecialFolder.Favorites:
                    return AppDataPaths.GetDefault().Favorites;
                case SpecialFolder.History:
                    return AppDataPaths.GetDefault().History;
                case SpecialFolder.InternetCache:
                    return AppDataPaths.GetDefault().InternetCache;
                case SpecialFolder.MyMusic:
                    return UserDataPaths.GetDefault().Music;
                case SpecialFolder.MyPictures:
                    return UserDataPaths.GetDefault().Pictures;
                case SpecialFolder.MyVideos:
                    return UserDataPaths.GetDefault().Videos;
                case SpecialFolder.Recent:
                    return UserDataPaths.GetDefault().Recent;
                case SpecialFolder.System:
                    return SystemDataPaths.GetDefault().System;
                case SpecialFolder.Templates:
                    return UserDataPaths.GetDefault().Templates;
                case SpecialFolder.DesktopDirectory:
                    return UserDataPaths.GetDefault().Desktop;
                case SpecialFolder.Personal:
                    return UserDataPaths.GetDefault().Documents;
                case SpecialFolder.CommonDocuments:
                    return SystemDataPaths.GetDefault().PublicDocuments;
                case SpecialFolder.CommonMusic:
                    return SystemDataPaths.GetDefault().PublicMusic;
                case SpecialFolder.CommonPictures:
                    return SystemDataPaths.GetDefault().PublicPictures;
                case SpecialFolder.CommonDesktopDirectory:
                    return SystemDataPaths.GetDefault().PublicDesktop;
                case SpecialFolder.CommonVideos:
                    return SystemDataPaths.GetDefault().PublicVideos;
                case SpecialFolder.UserProfile:
                    return UserDataPaths.GetDefault().Profile;
                case SpecialFolder.SystemX86:
                    return SystemDataPaths.GetDefault().SystemX86;
                case SpecialFolder.Windows:
                    return SystemDataPaths.GetDefault().Windows;

                // The following aren't available on WinRT. Our default behavior
                // is string.Empty for paths that aren't available.
                //
                // case SpecialFolder.Programs:
                // case SpecialFolder.MyComputer:
                // case SpecialFolder.SendTo:
                // case SpecialFolder.StartMenu:
                // case SpecialFolder.Startup:
                // case SpecialFolder.ProgramFiles:
                // case SpecialFolder.CommonProgramFiles:
                // case SpecialFolder.AdminTools:
                // case SpecialFolder.CDBurning:
                // case SpecialFolder.CommonAdminTools:
                // case SpecialFolder.CommonOemLinks:
                // case SpecialFolder.CommonStartMenu:
                // case SpecialFolder.CommonPrograms:
                // case SpecialFolder.CommonStartup:
                // case SpecialFolder.CommonTemplates:
                // case SpecialFolder.Fonts:
                // case SpecialFolder.NetworkShortcuts:
                // case SpecialFolder.PrinterShortcuts:
                // case SpecialFolder.CommonProgramFilesX86:
                // case SpecialFolder.ProgramFilesX86:
                // case SpecialFolder.Resources:
                // case SpecialFolder.LocalizedResources:

                default:
                    return string.Empty;
            }
        }

        private static string GetFolderPathCoreFallBack(SpecialFolder folder)
        {
            // For testing without the new WinRT APIs. We cannot use Win32 APIs for
            // special folders as they are not in the WACK.
            switch (folder)
            {
                case SpecialFolder.ApplicationData:
                    return ApplicationData.Current.RoamingFolder?.Path ?? string.Empty;
                case SpecialFolder.LocalApplicationData:
                    return ApplicationData.Current.LocalFolder?.Path ?? string.Empty;
                case SpecialFolder.System:
                    return SystemDirectory;
                case SpecialFolder.Windows:
                    return Path.GetDirectoryName(SystemDirectory)!;
                default:
                    return string.Empty;
            }
        }
    }
}