using System;
using System.Drawing;
using System.Windows.Forms;
class MainClass{
public static void Main()
{
Console.WriteLine(SystemInformation.ArrangeDirection);
}
}
Month: February 2011
Demonstrates using the Microsoft.SystemEvents class to intercept an event generated by the system
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Event.cs -- Demonstrates using the Microsoft.SystemEvents class to intercept
// an event generated by the system.
//
// Compile this program with the following command line:
// C:>csc Event.cs
//
namespace nsEvent
{
using System;
using System.Windows.Forms;
using Microsoft.Win32;
public class Event
{
//
// Define the delegate
public delegate void UserEventHandler (object obj, UserPreferenceChangedEventArgs args);
//
// Declare a variable that will hold the delegate
static public event UserEventHandler ShowEvent;
static public void Main ()
{
//
// Create the delegate using the event handler (below)
ShowEvent = new UserEventHandler (EvHandler);
//
// Creeate the event handler using the new operator
UserPreferenceChangedEventHandler handler = new UserPreferenceChangedEventHandler(ShowEvent);
//
// Add the delegate to the system delegate list. This is a multi-cast delegate
// and you must use the += operator to add the delegate. Use the -= operator
// to remove the delegate
SystemEvents.UserPreferenceChanged += handler;
//
// Show a message box to keep the program alive while you cause an event
MessageBox.Show ("Hey! C Sharp", "System Events");
//
// Remove the delegate from the system delegate list
SystemEvents.UserPreferenceChanged -= handler;
}
//
// Declare and define the method that will be used as the event handler function
static void EvHandler (object obj, UserPreferenceChangedEventArgs args)
{
/* Retrieve the category of the change */
UserPreferenceCategory cat = args.Category;
/* Build a string for the message box */
string str = "User changed the " + cat.ToString() + " category";
/* Show the change event */
MessageBox.Show (str, " event category");
}
}
}
System Functions: LogOff, Restart, Shutdown, Hibernate, Standby
// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
#if !SILVERLIGHT
using System.Windows.Forms;
#endif
namespace crudwork.Utilities
{
/// <summary>
/// System Functions: LogOff, Restart, Shutdown, Hibernate, Standby
/// </summary>
public static class SystemFunctions
{
[DllImport("user32.dll")]
private static extern void LockWorkStation();
[DllImport("user32.dll")]
private static extern int ExitWindowsEx(int uFlags, int dwReason);
private enum RecycleFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
}
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
private static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
/// <summary>
/// Empty the System Recycle Bin
/// </summary>
public static uint EmptyRecycleBin()
{
return SHEmptyRecycleBin(IntPtr.Zero, null, 0);
}
/// <summary>
/// Lock the workstation
/// </summary>
public static void Lock()
{
LockWorkStation();
}
/// <summary>
/// Log off the user
/// </summary>
/// <param name="force"></param>
public static void LogOff(bool force)
{
ExitWindowsEx(force ? 4 : 0, 0);
}
/// <summary>
/// Reboot the system
/// </summary>
public static void Reboot()
{
ExitWindowsEx(2, 0);
}
/// <summary>
/// Shutdown the system
/// </summary>
public static void Shutdown()
{
ExitWindowsEx(1, 0);
}
#if !SILVERLIGHT
/// <summary>
/// Put the system into hibernate mode
/// </summary>
public static void Hibernate()
{
Hibernate(true, true);
}
/// <summary>
/// Put the system into hibernate mode
/// </summary>
/// <param name="force"></param>
/// <param name="disableWakeupEvent"></param>
public static void Hibernate(bool force, bool disableWakeupEvent)
{
Application.SetSuspendState(PowerState.Hibernate, force, disableWakeupEvent);
}
/// <summary>
/// Put the system into standby mode
/// </summary>
public static void Standby()
{
Standby(true, true);
}
/// <summary>
/// Put the system into standby mode
/// </summary>
/// <param name="force"></param>
/// <param name="disableWakeupEvent"></param>
public static void Standby(bool force, bool disableWakeupEvent)
{
Application.SetSuspendState(PowerState.Suspend, force, disableWakeupEvent);
}
#endif
}
}
StringWriter/Reader
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
class Program {
static void Main(string[] args) {
StringWriter strWriter = new StringWriter();
strWriter.WriteLine("Don't forget Mother's Day this year...");
strWriter.Close();
Console.WriteLine(strWriter);
StringBuilder sb = strWriter.GetStringBuilder();
sb.Insert(0, "Hey!! ");
Console.WriteLine("-> {0}", sb.ToString());
sb.Remove(0, "Hey!! ".Length);
Console.WriteLine("-> {0}", sb.ToString());
StringReader strReader = new StringReader(strWriter.ToString());
string input = null;
while ((input = strReader.ReadLine()) != null) {
Console.WriteLine(input);
}
strReader.Close();
}
}
Replace Char with String
using System;
using System.Text;
public class MainClass {
static String ReplaceCharString(String s, char c1, String s2) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (s[i] == c1)
res.Append(s2);
else
res.Append(s[i]);
return res.ToString();
}
}
[/csharp]
Replace char in a StringBuilder object
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
StringBuilder greetingBuilder = new StringBuilder("www.kutayzorlu.com/java2s/com");
for (int i = (int)'z'; i >= (int)'a'; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingBuilder = greetingBuilder.Replace(old1, new1);
}
for (int i = (int)'Z'; i >= (int)'A'; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingBuilder = greetingBuilder.Replace(old1, new1);
}
Console.WriteLine("Encoded:
" + greetingBuilder.ToString());
}
}
using the StringBuilder methods Replace, Insert, Append, AppendFormat, and Remove:
using System;
using System.Text;
class UseSBApp {
static void Main(string[] args) {
StringBuilder sb = new StringBuilder("Pineapple");
sb.Replace('e', 'X');
sb.Insert(4, "Banana");
sb.Append("Kiwi");
sb.AppendFormat(", {0}:{1}", 123, 45.6789);
sb.Remove(sb.Length - 3, 3);
Console.WriteLine(sb);
}
}

