Test T
Test T

Reputation: 11

How to give each local user an unique ID? C#

I'm working on a personal project to kill some time when I'm bored. So I have this login + register application which saves all data locally. Each generated user gets it's own map with his login credentials located in a text file.

However I'm having troubles giving each map an unique ID, this is my code for so far and I can't manage to make it work.

            const int User_ID = 0;
            for (int i = 0; i < i; i++)
            {

            }

            Directory.CreateDirectory("data\\" + User_ID);

            var sw = new StreamWriter("data\\" + User_ID + "\\data.txt");

            sw.WriteLine(User_ID);
            sw.Close();

Can someone help me out?

Upvotes: 1

Views: 148

Answers (2)

David Chelliah
David Chelliah

Reputation: 1349

Generating a GloballyUniqueIdentifier (GUID) will be the best approach because you need not think about the earlier UserIds generated so far by your application. Because GUIDs are guaranateed to be created without collision

string User_ID = Guid.NewGuid().ToString();
for (int i = 0; i < i; i++)
{
}

Directory.CreateDirectory("data\\" + User_ID);
var sw = new StreamWriter("data\\" + User_ID + "\\data.txt");

sw.WriteLine(User_ID);
sw.Close();

Upvotes: 1

wake-0
wake-0

Reputation: 3968

Eventually you would use instead of an int a GUID for the user id:

Guid id = Guid.NewGuid();

Upvotes: 2

Related Questions