Wednesday, April 30, 2014

Easy way to search attribute value in XML

If there are many attributes with same name like below
<root>
      <node name="n1" path="1.jpg"/>
      <node name="n2" path="2.jpg"/>
      <node name="n3" path="3.jpg"/>
      <node name="n4" path="4.jpg"/>
</root>

Then if you want to search node n3 path value in easy method then use the following method

XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
doc.SelectSingleNode("/root/node[@name='n3']").Attributes["path"].Value

Sorting List in WPF C#

The easiest method to sort a List of string or any type do the follow

If List is string type

listData.Sort((a, b) => a.Count().CompareTo(b.Count()));

If List is any Class type

listData.Sort((a, b) => a.GameName.CompareTo(b.GameName));

Tuesday, April 15, 2014

Syntax Formatting for any Language in WPF C# Textbox

Its really Simple and Easy to format any languge
  1. First You need to install AvalonEdit from Manage NuGet Packages. Use following link to install instructions http://www.nuget.org/packages/AvalonEdit
or Just right click on Project in Solution Explorer and Click Manage NuGet Packages, now click Online and Search Avalon Editor. Then Install AvalonEditor
  1. Just add following code in xaml file.
    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
  2. Now add your TextEditor as follow
    <avalonedit:TextEditor SyntaxHighlighting="XML" x:Name="gameListXMLText" Height="200">
  3. Just Change SyntaxHighlighting as you want it could be C#
  4. Now Just load xml file as follow
gameListXMLText.Text = File.ReadAllText("sample.xml");

Check Multiple Key at a time in C#

I have an TextBox where I need Control Key and Plus / Minus Key to Change the Font Size. So I did the following steps for this.


            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                if (Keyboard.IsKeyDown(Key.OemMinus))
                    SmallSize_Click(null, null);
                else if (Keyboard.IsKeyDown(Key.OemPlus))
                    BigSize_Click(null, null);
            }

Tuesday, April 8, 2014

Get latest Directory from the Directory in C#

You can get latest file or folder path from this function

public string getLatestDirectory(string path, string folderName)
{
            FileInfo[] info = Directory.GetDirectories(path + "\\"+folderName).Select(x => new FileInfo(x)).OrderByDescending(x => x.LastWriteTime).Take(1).ToArray();
            return Path.GetFullPath(info[0].Name);
}