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

Page History: Code Snippets - .NET Framework

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Mon, Nov 23, 2009, 12:25 PM


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
   Other
         Hourglass/Default Cursor
         ASCII Codes
         New GUID
         Sleep
         Detecting Design-Time
         Resources
         Embedded Resources
         Threading
         Escaping XML Characters
         Time Remaining


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) {
}

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;
}

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