My online notes – Section 0008

Note 1: Remove diacritics (special characters) from string

private static string RemoveDiacritics(string text)
{
	string textNormalized = text.Normalize(NormalizationForm.FormD);
	StringBuilder stringBuilder = new StringBuilder();

	for (int index = 0; index < textNormalized.Length; index++)
	{
		UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(textNormalized[index]);
		if (uc != UnicodeCategory.NonSpacingMark)
		{
			stringBuilder.Append(textNormalized[index]);
		}
	}

	return (stringBuilder.ToString().Normalize(NormalizationForm.FormC));
}

Continue reading My online notes – Section 0008

My online notes – Section 0006

Note 1: Sony Ericsson PC Suite for Z610i
http://www.megaupload.com/?d=V7II5C41

Note 2: Set autostart for program

private Boolean SchreibeinAutostart( String dateipfad )
{
    try
    {
        String samAccountName;
        {
            var currentWindowsIdentity = WindowsIdentity.GetCurrent( );
            if ( currentWindowsIdentity == null )
            {
                return false;
            }

            samAccountName = currentWindowsIdentity.Name;
        }

        var registrySecurity = new RegistrySecurity( );
        {
            registrySecurity.AddAccessRule( new RegistryAccessRule( samAccountName,
                    RegistryRights.WriteKey | RegistryRights.ChangePermissions, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow ) );
        }

        var targetRegistryKey = Registry.CurrentUser.CreateSubKey( "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\" );

        if ( targetRegistryKey == null )
        {
            return false;
        }

        targetRegistryKey.SetValue( dateipfad.Remove( 0, dateipfad.LastIndexOf( @"\" ) + 1 ), dateipfad );

    }
    catch ( Exception )
    {
        return false;
    }

    return true;
}

Continue reading My online notes – Section 0006

My online notes – Section 0005

Note 1: Impersonation for accessing network path

private void ApplyUserCredentials(string Share, string Domain, string Username, string Password)
{
	USE_INFO_2 useInfo = new USE_INFO_2();
	useInfo.ui2_local = string.Empty;
	useInfo.ui2_remote = Share;
	useInfo.ui2_password = Password;
	useInfo.ui2_asg_type = 0;    //disk drive
	useInfo.ui2_usecount = 1;
	useInfo.ui2_username = Username;
	useInfo.ui2_domainname = Domain;
	uint paramErrorIndex;

	uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);
	if (returnCode != 0)
	{
		throw new Win32Exception((int)returnCode);
	}
}

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern UInt32 NetUseAdd(string UncServerName,UInt32 Level, ref USE_INFO_2 Buf,  out UInt32 ParmError);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct USE_INFO_2
{
	internal string ui2_local;
	internal string ui2_remote;
	internal string ui2_password;
	internal UInt32 ui2_status;
	internal UInt32 ui2_asg_type;
	internal UInt32 ui2_refcount;
	internal UInt32 ui2_usecount;
	internal string ui2_username;
	internal string ui2_domainname;
}

Continue reading My online notes – Section 0005

My online notes – Section 0004

Note 1: Proxy of HttpWebRequest for uploading

HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create(this.fullUrl);
<system.net>
    <defaultProxy enabled ="false">
		<proxy autoDetect ="True"/>
    </defaultProxy>
</system.net>

Note 2: Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control for page loading
http://msdn.microsoft.com/en-us/library/15a31akc.aspx

Continue reading My online notes – Section 0004

My online notes – Section 0003

Note 1: Check if current user is administrator

WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
Console.WriteLine("Programm wird unter dem Usercontext ausgeführt: {0}", principal.Identity.Name);
Console.WriteLine("Adminrechte vorhanden: {0}", principal.IsInRole(WindowsBuiltInRole.Administrator) ? "Ja" : "Nein");

Note 2: ContextMenuStrip at selected row open and Index in DataTable localize.

private void dgv_MouseUp(object sender, MouseEventArgs e)
{
  DataGridView.HitTestInfo hitTest = ((DataGridView)sender).HitTest(e.X, e.Y);

  if (hitTest .Type == DataGridViewHitTestType.Cell)
  {
     dgv.CurrentCell = dgv.Rows[hTest.RowIndex].Cells[hTest.ColumnIndex];
  }
}

Continue reading My online notes – Section 0003

My online notes – Section 0002

Note 1: Wait from GUI Thread

private void cmdStart_Click(object sender, RoutedEventArgs e)
{
	thr = new Thread(new ParameterizedThreadStart(Test));
	thr.Start(this);
}

public void Test(object obj)
{
	var wnd = (MainWindow)obj;
	int i = 0;
	while (true)
	{
		i++;
		if (i % 1000000 == 0) //Ab ca. 1000000 schafft die UI es
		{
			var __op = wnd.Dispatcher.BeginInvoke(new Action(UpdateGUI));
			__op.Wait(); //Magische Methode mit der der Arbeitsthread wartet?!
		}
	}
}

public void UpdateGUI()
{
	Random r = new Random();

	txt.Text = "";
	txt.Text = ((char)r.Next(65, 91)).ToString();
}
		

Continue reading My online notes – Section 0002

My online notes – Section 0001

Note 1:
MongoDB Applied Design Patterns
https://www.dropbox.com/s/5nksr6e8z09mu83/Oreilly.MongoDB.Applied.Design.Patterns.Mar.2013.epub?dl=0

Peopleware
https://www.dropbox.com/s/4v3j51mkt13yusc/Peopleware.epub?dl=0


Note 2:

static PerformanceCounter cpuUsage;
public static void Main(string[] args)
{
	cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");
	Timer t = new Timer(new TimerCallback(TimerFunction), null, 0, 500);
	Console.ReadLine();
}

static void TimerFunction(Object stateInfo)
{
	//AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
	Console.WriteLine(cpuUsage.NextValue() + " %");
}

Continue reading My online notes – Section 0001