Environment.UserName プロパティ

定義

現在のスレッドに関連付けられているユーザーのユーザー名を取得します。

public:
 static property System::String ^ UserName { System::String ^ get(); };
public static string UserName { get; }
static member UserName : string
Public Shared ReadOnly Property UserName As String

プロパティ値

現在のスレッドに関連付けられているユーザーのユーザー名。

次の例では、現在のスレッドを開始したユーザーのユーザー名を表示します。

// Sample for the Environment.UserName property
using System;

class Sample
{
    public static void Main()
    {
    Console.WriteLine();
//  <-- Keep this information secure! -->
    Console.WriteLine("UserName: {0}", Environment.UserName);
    }
}
// Sample for the Environment.UserName property
open System

//  <-- Keep this information secure! -->
printfn $"\nUserName: {Environment.UserName}"
' Sample for the Environment.UserName property
Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      '  <-- Keep this information secure! -->
      Console.WriteLine("UserName: {0}", Environment.UserName)
   End Sub
End Class

注釈

UserName プロパティを使用すると、セキュリティまたはアクセスの目的で、システムとアプリケーションに対して、現在のスレッド上のユーザーを識別できます。 また、ユーザーごとに特定のアプリケーションをカスタマイズするためにも使用できます。

WindowsUserName プロパティは、Windows GetUserName 関数の呼び出しをラップします。 ユーザーのドメイン アカウントの資格情報は、ユーザーのドメイン名、'\' 文字、およびユーザー名として書式設定されます。 UserDomainName プロパティを使用してユーザーのドメイン名を取得し、UserNameプロパティを使用してユーザー名を取得します。

Unix プラットフォームでは、 UserName プロパティは getpwuid_r 関数の呼び出しをラップします。

ASP.NET アプリケーションが開発環境で実行されている場合、UserName プロパティは現在のユーザーの名前を返します。 発行された ASP.NET アプリケーションでは、このプロパティはアプリケーション プール アカウントの名前 (Default AppPool など) を返します。

適用対象

こちらもご覧ください