Anastasios Moraitis
Anastasios Moraitis

Reputation: 388

How to get Windows Store(not local) Account Name in UWP?

I m currently developing a UWP app, and i want to get the user's Name, from the store(for analytics).

I want to save it in a database and update the DateTime when the app opens, which will give me the app insights. How i can get the User's name on Windows Store Account(or a unique ID to separate the User from others). I dont want to save the local account name, as it described at other quesions here. Thanks in advance.

Upvotes: 0

Views: 96

Answers (1)

Florian Moser
Florian Moser

Reputation: 2663

Use the ApplicationData.Current.RoamingFolder to store information you want to persist for your user. To be able to distinguish between different users save a unique id to this folder, and you're good to go!

public async Task<string> GetUserIdAsync()
{
    var fileName = "user_id";
    var folder = ApplicationData.Current.RoamingFolder;
    var file = await folder.TryGetItemAsync(fileName);
    if (file == null)
    {
        //if file does not exist we create a new guid
        var storageFile = await folder.CreateFileAsync(fileName);
        var newId = Guid.NewGuid().ToString();
        await FileIO.WriteTextAsync(storageFile, newId);
        return newId;
    }
    else
    {
        //else we return the already exising guid
        var storageFile = await folder.GetFileAsync(fileName);
        return await FileIO.ReadTextAsync(storageFile);
    }
}

Upvotes: 1

Related Questions