Environment.UserName 屬性

定義

取得與目前執行緒建立關聯的人員使用者名稱。

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

屬性值

String

與目前線程相關聯的人員使用者名稱。

範例

下列範例會顯示啟動目前線程之人員的使用者名稱。

// Sample for the Environment::UserName property
using namespace System;
int main()
{
   Console::WriteLine();

   //  <-- Keep this information secure! -->
   Console::WriteLine( "UserName: {0}", Environment::UserName );
}
// 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 屬性來識別目前線程上的使用者,以取得安全性和應用程式的安全性或存取目的。 它也可以用來自訂每個使用者的特定應用程式。

在 Windows 屬性會 UserName 包裝對 Windows GetUserName函式的呼叫。 使用者的網域帳號憑證會格式化為使用者的功能變數名稱、' \ ' 字元和使用者名稱。 UserDomainName使用 屬性來取得使用者的功能變數名稱和 UserName 屬性,以取得使用者名稱。

在 Unix 平臺上,屬性會 UserName 包裝函式的 getpwuid_r 呼叫。

如果 ASP.NET 應用程式在開發環境中執行,則 UserName 屬性會傳回目前使用者的名稱。 在已發佈 ASP.NET 應用程式中,此屬性會傳回應用程式集區帳戶的名稱 (,例如預設 AppPool) 。

適用於

另請參閱