Asked  7 Months ago    Answers:  5   Viewed   36 times

How do I get the current username in .NET using C#?

 Answers

18
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Tuesday, June 1, 2021
 
Kevin_Kinsey
answered 7 Months ago
10

I found it:

$env:UserName

There is also:

$env:UserDomain
$env:ComputerName
Thursday, June 3, 2021
 
SuperString
answered 7 Months ago
18

You must use the following:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];

There is no difference between NSDate* now and NSDate *now, it's just a matter of preference. From the compiler perspective, nothing changes.

Saturday, July 10, 2021
 
FyodorX
answered 5 Months ago
59

Are you doing this at work? Is a corporate firewall or anti-virus program running? Check the windows event logs and any applications in your task notification icons area on your start bar for hints as to who is blocking this.

Wednesday, August 25, 2021
 
panthro
answered 4 Months ago
59

If WCF meets your needs, it's worth looking at. ZeroC and other alternative higher level libraries exist. Otherwise there are several different ways to work closer to the socket level if that's what you need.

TcpClient/UdpClient

These provide a relatively thin wrapper around the underlying sockets. It essentially provides a Stream over the socket. You can use the async methods on the NetworkStream (BeginRead, etc.). I don't like this one as the wrapper doesn't provide that much and it tends to be a little more awkward than using the socket directly.

Socket - Select

This provides the classic Select technique for multiplexing multiple socket IO onto a single thread. Not recommended any longer.

Socket - APM Style

The Asynchronous Programming Model (AKA IAsyncResult, Begin/End Style) for sockets is the primary technique for using sockets asynchronously. And there are several variants. Essentially, you call an async method (e.g., BeginReceive) and do one of the following:

  1. Poll for completion on the returned IAsyncResult (hardly used).
  2. Use the WaitHandle from the IAsyncResult to wait for the method to complete.
  3. Pass the BeginXXX method a callback method that will be executed when the method completes.

The best way is #3 as it is the usually the most convenient. When in doubt, use this method.

Some links:

  • MSDN Magazine Article on Sockets
  • A Jeffery Richter Article on the Asynchronous Programming Model

.NET 3.5 High Performance Sockets

.NET 3.5 introduced a new model for async sockets that uses events. It uses the "simplified" async model (e.g., Socket.SendAsync). Instead of giving a callback, you subscribe to an event for completion and instead of an IAsyncResult, you get SocketAsyncEventArgs. The idea is that you can reuse the SocketAsyncEventArgs and pre-allocate memory for socket IO. In high performance scenarios this can be much more efficient that using the APM style. In addition, if you do pre-allocate the memory, you get a stable memory footprint, reduced garbage collection, memory holes from pinning etc. Note that worrying about this should only be a consideration in the most high performance scenarios.

  • MSDN Magazine: Get Connected With The .NET Framework 3.5
  • MSDN Information with technique for pre-allocating memory

Summary

For most cases use the callback method of the APM style unless you prefer the style of the SocketAsyncEventArgs / Async method. If you've used CompletionPorts in WinSock, you should know that both of these methods use CompletionPorts under the hood.

Friday, September 10, 2021
 
Rüdiger Schulz
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share