开发者

Do Unique Instance Session ID's Live Inside Of Programs?

I am looking for a variable that I can just call from C#, like Environment.Username that is unique to the instance of a particular application (like a timestamp for when the application was initially opened 开发者_运维技巧or something).

If this is available, that would make my project a lot easier. Thanks!


Process.GetCurrentProcess().Id

is unique for each instance of an application process. It does change every time the application is restarted though :)


You could use a Guid.NewGuid(), which is unique. If you need it for the lifetime of your application, you could use

public static Guid ApplicationId = Guid.NewGuid();

which would get populated the first time this type is used.


I don't know if I understand you right. Don't you need this?

public static class MyClass {
    // fore every process this value will be unique, but same for whole "life" of process
    private static readonly Guid unique = Guid.NewGuid();
    public static Guid Unique { get { return unique; } }
}


Every process under Windows has a unique Process ID. You can access it via the Process.Id property. To obtain an instance of Process corresponding to the currently running instance of your program call Process.GetCurrentProcess. Every instance of your program will be represented as a separate process in Windows, and hence will receive a different (and unique) Process ID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜