Local file storage during development of a Windows Store App

半世苍凉 提交于 2020-01-06 13:53:34

问题


So I'm getting started in windows store apps. It is also my first experience with Visual Studio. Windows 8.1 for that matter, too :P

I'm trying to figure out how local file storage works during the dev phase of this project. I understand the concept of how the appdata directory and the application install directory interact with the app once it is published and installed on a client's PC. This SO answer gives a good overview of how it is supposed to work. However, I can't figure out how, during development, I am supposed to use these concepts if the app isn't installed and doesn't have a dedicated folder in any of the appdata directories.

Any help is much appreciated!


回答1:


As others have pointed out, the app will be installed locally and the app's data will reside in some common place like C:\Users\YourUserName\AppData\local\packages during development. Check with the Packaging tab of your Package.appxmanifest file to determine the Package family name, it's probably going to be something cryptic. There'll be a corresponding folder in the packages folder.

One additional thing, though. There's a difference between an app that's been installed from an .appx file (e.g., through the store or side-loaded onto a device), and the type of installation that Visual Studio will perform for you when debugging. The latter is a so-called "unpackaged installation", and the application files that are part of your app package will be laid out slightly differently, with different implications. Most importantly, you will be able to modify the contents of files in your app package, which will fail for the same app when installed from an .appx. Also, you won't be able to install a packaged version of an app if an unpackaged install is present on the machine. (Of course, files in the AppData folder will behave the same way in either case.)




回答2:


Where are the files ?

As @Chuck Walbourn mentionned your application should create any initial versions of AppData files and subdirectories it needs if they are not present.

Usually it resides in C:\Users\UserName\AppData\local\packages\EncryptedNameLinkedToYourApplication

To be sure that it is really situated there add this line into your code and make sure your application executes it :

Dim folder = ApplicationData.Current.LocalFolder

Use debugging

You can then do a breakpoint (click on the grayed out margin at the left of the newly added line of code).

Once the breakpoint is triggered hover your mouse over folder and a popup will appear. Inside that popup you can access the path property which will give you the full path of the local files on your computer.

You then know what is the "EncryptedNameLinkedToYourApplication" mentionned earlier

Too lazy to use a breakpoint?

Then, in your application, add 1 button and 1 textbox. On the click of the button you then want to add this code :

Dim folder = ApplicationData.Current.LocalFolder
TextBoxTest.Text = folder.Path

Your textbox is now filled with the path to your local files including the encrypted application name




回答3:


When you build and debug the Windows Store app, Visual Studio's deployment is installing the app and running it as if it were deployed through the Store or side-loaded.

As with User Account Controller, your application should create any initial versions of AppData files and subdirectories it needs if they are not present.

See File access and permissions (Windows Runtime apps)

EDIT: For Windows Store apps, you get the path to the directories you can use from Windows::Storage::ApplicationData and the Current->LocalFolder, Current->RoamingFolder, or Current->TemporaryFolder properties.

For Windows desktop apps, you used either the Windows 2000/XP SHGetFolderPath Win32 API or the Windows Vista era IKnownFolder COM API (which has an easier to use wrapper SHGetKnownFolderPath). The Dual-use Coding Techniques for Games post shows some examples of writing both versions.

#include <wrl\client.h>

using Microsoft::WRL::ComPtr;

void GetApplicationDataDirectory(wchar_t* dir, size_t maxsize)
{
    if (!maxsize) return;
    *dir = 0;
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
    // You can use the Win32 SHGetKnownFolderPath as well which is just
    // a wrapper that does the same thing.

    // On Windows XP, you use the older SHGetFolderPath function with
    // different constants that have the same meaning.

    ComPtr<IKnownFolderManager> mgr;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager,
        nullptr, CLSCTX_INPROC_SERVER, IID_IKnownFolderManager, (LPVOID*) &mgr);
    if (SUCCEEDED(hr))
    {
        ComPtr<IKnownFolder> folder;
        hr = mgr->GetFolder(FOLDERID_LocalAppData, &folder);
        if (SUCCEEDED(hr))
        {
            LPWSTR szPath = 0;
            hr = folder->GetPath(0, &szPath);
            if (SUCCEEDED(hr))
            {
                // A big different with Windows desktop apps is here.
                // With Windows Store apps, your appdata directory is 
                // isolated per-user and per-app. In Windows desktop,
                // it is only isolated per-user so you have to create
                // your own unique subdir and other Windows desktop apps
                // can mess with your data
                wcscpy_s(dir, maxsize, szPath);
                wcscat_s(dir, maxsize, L”\\MyUniqueApplicationName”);
                CreateDirectory(dir, nullptr);
                CoTaskMemFree(szPath);
            }
        }
    }
#else // Windows Store WinRT app
    auto folder = Windows::Storage::ApplicationData::Current
        ->LocalFolder;
    wcscpy_s(dir, maxsize, folder->Path->Data());
#endif
}


来源:https://stackoverflow.com/questions/30533869/local-file-storage-during-development-of-a-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!