Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove characters \ words in string

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
hello,

I'm new to C# and in the early stages of learning. I have the following code that retrieves the domain and user. I like to remove the domain from the string result leaving only the user name

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Security.Principal;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Microsoft.Win32.RegistryKey key;

        private void Form1_Load(object sender, System.EventArgs e)
        {
            string sCurrentUser;
            sCurrentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            label1.Text = sCurrentUser;            
        }
    }
}

This is what I get
Code:
Domain\UserName

This is what I like to get
Code:
UserName

Thanks for any help with this
 
using System.Security.Principal;
using System.Linq;
...

var nameOnly = WindowsIdentity.GetCurrent().Name.Split(@'\').Last();

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top