Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Code Snippets - .NET Framework

RSS
Modified on Thu, Jun 28, 2012, 3:24 PM by Administrator Categorized as XML, XSL, and XPath, ┬ĚNet Framework
All the following code snippets are written in C#, although some of them can be copied verbatim into Visual Basic .NET. You can convert code between C# and VB.NET at this website.. The information on Reflection has been moved to its own page.

Table of Contents [Hide/Show]


   Environment
         Username
         Computer Name
         Computer Name, Fully Qualified with Domain Name
   Network
         IP Address
         Ping Operation
         Default Gateway
   Other
         Hourglass/Default Cursor
         ASCII Codes
         New GUID
         Sleep
         Detecting Design-Time
         Resources
         Embedded Resources
         Threading
         Escaping XML Characters
         Time Remaining
         Calling Method


Environment

Username

//for Windows applications
System.Environment.UserName 
//for Web applications
HttpContext.Current.User.Identity.Name


Computer Name

 // Option #1
System.Environment.MachineName
// Option #2
System.Net.Dns.GetHostName()

Computer Name, Fully Qualified with Domain Name

System.Net.Dns.GetHostEntry(Environment.MachineName).HostName

Network

IP Address

System.Net.Dns.GetHostAddresses(string hostName)[0]

Ping Operation

using System.Net.NetworkInformation;

. . .

try {
    new Ping().Send(address).Status == IPStatus.Success
}
catch (Exception ex) {
}

Default Gateway

using System.Net.NetworkInformation;
using System.Net;

. . .

List<IPAddress> addresses = new List<IPAddress>();

foreach (NetworkInterface networkCard in NetworkInterface.GetAllNetworkInterfaces())
{
    GatewayIPAddressInformationCollection gateways 
        = networkCard.GetIPProperties().GatewayAddresses;

    foreach (GatewayIPAddressInformation gateway in gateways)
        addresses.Add(gateway.Address);
}

Other

Hourglass/Default Cursor

this.Cursor = Cursors.WaitCursor;
this.Cursor = Cursors.Default;

ASCII Codes

char c; 
int asciiCode = (int)c;

New GUID

System.Guid.NewGuid().ToString()

Sleep

System.Threading.Thread.Sleep(ms)

Detecting Design-Time

System.Diagnostics.Debugger.IsAttached = true;

Resources

picHide.Image = (System.Drawing.Image)

// Note that resourceName IS CASE-SENSITIVE!
Resources.ResourceManager.GetObject(resourceName);

Embedded Resources

Cursor result = null;
/* resourceName follows the format
 *    Namespace.Folder.Resources.FileName  */
string resourceName = "MyApp.Resources.SelectTextLine.cur";
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

if (s != null)
    result = new Cursor(s);

Threading

using System.Threading;

// starter.Start is the name of the object/method to call
Thread t = new Thread(new ThreadStart(starter.Start));
t.Start();

Escaping XML Characters

System.Security.SecurityElement.Escape(inputText);

Time Remaining

private static TimeSpan CalcTimeRemaining(int n, int m, TimeSpan elapsed)
{
    double rate = elapsed.TotalMilliseconds / (double)n;
    double ms = ((double)m - (double)n) * rate;
    long ticks = (long)(ms * 10000);
    TimeSpan remaining = new TimeSpan(ticks);
    return remaining;
}

Calling Method

System.Reflection.MethodBase caller = new System.Diagnostics.StackTrace(1, false).GetFrame(0).GetMethod();
string s = caller.Name;

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.