Display DateTime up to milliseconds
string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"); Console.WriteLine(myTime);
11391 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Miroslav Stampar http://mstampar.awardspace.com
string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"); Console.WriteLine(myTime);
RegistryKey reg = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop"); reg.SetValue("WallpaperStyle", "1"); //2 for stretch
RegistryKey reg = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); reg.SetValue("WallpaperStyle", "1"); //2 for stretch
public struct SystemTime { public ushort Year; public ushort Month; public ushort DayOfWeek; public ushort Day; public ushort Hour; public ushort Minute; public ushort Second; public ushort Millisecond; }; [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)] public extern static void Win32GetSystemTime(ref SystemTime st); [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)] public extern static bool Win32SetSystemTime(ref SystemTime st); .... public static void Test() { SystemTime newTime = new SystemTime(); newTime.Year = (ushort)2005; newTime.Month = (ushort)12; newTime.Day = (ushort)2; newTime.Hour = (ushort)12; //UTC time (if you are in UTC+2 zone then you'll put here: time - 2h) newTime.Minute = (ushort)42; newTime.Second = (ushort)11; Win32SetSystemTime(ref newTime); }
public static void RemoveEmptyNodes(XmlDocument doc) { XmlNodeList nodes = doc.SelectNodes("//node()"); foreach (XmlNode node in nodes) if ((node.Attributes.Count == 0) && (node.ChildNodes.Count == 0)) node.ParentNode.RemoveChild(node); }
string filename = "c:\\sample.htm"; FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None); //locks file ... stream.Close(); //unlocks file
string filename = "c:\\sample.htm"; FileStream stream = File.Open(filename, FileMode.Open); stream.Lock(0, stream.Length); //locks file ... stream.Unlock(0, stream.Length); //unlocks file
XmlNodeList list = xmlDoc.SelectNodes("//comment()"); foreach(XmlNode node in list) node.ParentNode.RemoveChild(node);
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}
// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;
// For generating random numbers.
private Random random = new Random();
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// ====================================================================
public CaptchaImage(int length, int width, int height) : this(length, width, height, null)
{
}
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ====================================================================
public CaptchaImage(int length, int width, int height, string familyName)
{
this.text = GenerateRandomText(length);
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}
private string GenerateRandomText(int length)
{
Random rnd = new Random();
List<char> table = new List<char>();
for (char i = 'A'; i <= 'Z'; i++)
table.Add(i);
for (char i = '0'; i <= '9'; i++)
table.Add(i);
string retVal = "";
for (int i = 0; i < length; i++)
retVal += table[rnd.Next(table.Count)];
return retVal;
}
// ====================================================================
// This member overrides Object.Finalize.
// ====================================================================
~CaptchaImage()
{
Dispose(false);
}
// ====================================================================
// Releases all resources used by this object.
// ====================================================================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
// ====================================================================
// Custom Dispose method to clean up unmanaged resources.
// ====================================================================
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Dispose();
}
// ====================================================================
// Sets the image width and height.
// ====================================================================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}
// ====================================================================
// Sets the font used for the image text.
// ====================================================================
private void SetFamilyName(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}
// ====================================================================
// Creates the bitmap image.
// ====================================================================
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);
// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
g.FillRectangle(hatchBrush, rect);
// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);
// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
g.FillPath(hatchBrush, path);
// Add some random noise.
int m = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
{
int x = this.random.Next(rect.Width);
int y = this.random.Next(rect.Height);
int w = this.random.Next(m / 50);
int h = this.random.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}
// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
// Set the image.
this.image = bitmap;
}
}
webBrowser1.Navigate("about:blank"); webBrowser1.Document.Write("<html><body><h1>Hello World!</h1></body></html>");
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleApplication12 { class Program { static object thisLock = new object(); static int z = 0; private static void ThrStart() { for (int i = 0; i < 10000; i++) { lock (thisLock) { z++; Console.WriteLine(z); } } } private static void ThrStart2() { for (int i = 0; i < 10000; i++) { Monitor.Enter(thisLock); z++; Console.WriteLine(z); Monitor.Exit(thisLock); } } static Mutex mutex = new Mutex(); private static void ThrStart3() { for (int i = 0; i < 10000; i++) { mutex.WaitOne(); z++; Console.WriteLine(z); mutex.ReleaseMutex(); } } static void Main(string[] args) { Thread t1 = new Thread(new ThreadStart(ThrStart)); Thread t2 = new Thread(new ThreadStart(ThrStart)); t1.Start(); t2.Start(); t1.Join(); t2.Join(); t1 = new Thread(new ThreadStart(ThrStart2)); t2 = new Thread(new ThreadStart(ThrStart2)); t1.Start(); t2.Start(); t1.Join(); t2.Join(); t1 = new Thread(new ThreadStart(ThrStart3)); t2 = new Thread(new ThreadStart(ThrStart3)); t1.Start(); t2.Start(); t1.Join(); t2.Join(); } } }
this.PointToScreen(this.Label1.Location)