четверг, 10 февраля 2011 г.

Convert files from Dos to Unix and back.


        private void Unix2Dos(string fileName)
        {
            const byte CR = 0x0D;
            const byte LF = 0x0A;
            byte[] DOS_LINE_ENDING = new byte[] { CR, LF };
            byte[] data = File.ReadAllBytes(fileName);
            using (FileStream fileStream = File.OpenWrite(fileName))
            {
                BinaryWriter bw = new BinaryWriter(fileStream);
                int position = 0;
                int index = 0;
                do
                {
                    index = Array.IndexOf<byte>(data, LF, position);
                    if (index >= 0)
                    {
                        if ( ( index > 0 ) && (data[index - 1] == CR ))
                        {
                            // already dos ending
                            bw.Write(data, position, index - position + 1);
                        }
                        else
                        {
                            bw.Write(data, position, index - position);
                            bw.Write(DOS_LINE_ENDING);
                        }
                        position = index + 1;
                    }
                }
                while (index > 0);
                bw.Write(data, position, data.Length - position);
               fileStream.SetLength(fileStream.Position);
            }
        }

        private void Dos2Unix(string fileName)
        {
            const byte CR = 0x0D;
            const byte LF = 0x0A;
            byte[] data = File.ReadAllBytes(fileName);
            using (FileStream fileStream = File.OpenWrite(fileName))
            {
                BinaryWriter bw = new BinaryWriter(fileStream);
                int position = 0;
                int index = 0;
                do
                {
                    index = Array.IndexOf<byte>(data, CR, position);
                    if ((index >= 0) && (data[index + 1] == LF))
                    {
                        // Write before the CR
                        bw.Write(data, position, index - position);
                        // from LF
                        position = index + 1;
                    }
                }
                while (index > 0);
                bw.Write(data, position, data.Length - position);
                fileStream.SetLength(fileStream.Position);
            }
        }

воскресенье, 6 февраля 2011 г.

DataGrid groupping snippet


What we should get:



XAML:
&lt;Window x:Class="WpfDataGrid1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"&gt;
    &lt;Grid&gt;
        &lt;DataGrid AutoGenerateColumns="False" Margin="15,40,14,12" Name="dataGrid1"&gt;
            &lt;DataGrid.GroupStyle&gt;
                &lt;GroupStyle&gt;
                    &lt;GroupStyle.ContainerStyle&gt;
                        &lt;Style  TargetType="{x:Type GroupItem}"&gt;
                        &lt;Setter Property="Template"&gt;
                            &lt;Setter.Value&gt;
                                &lt;ControlTemplate&gt;
                                    &lt;Expander x:Name="exp" IsExpanded="True"
                                            Background="White"
                                            Foreground="Black"&gt;
                                        &lt;Expander.Header&gt;
                                            &lt;TextBlock Text="{Binding Name}"/&gt;
                                        Expander.Header&gt;
                                        &lt;ItemsPresenter /&gt;
                                    Expander&gt;
                                ControlTemplate&gt;
                            Setter.Value&gt;
                        Setter&gt;
                        Style&gt;
                    GroupStyle.ContainerStyle&gt;
                    &lt;GroupStyle.Panel&gt;
                        &lt;ItemsPanelTemplate&gt;
                            &lt;DataGridRowsPresenter/&gt;
                        ItemsPanelTemplate&gt;
                    GroupStyle.Panel&gt;
                GroupStyle&gt;
            DataGrid.GroupStyle&gt;
            &lt;DataGrid.Columns&gt;
                &lt;DataGridTextColumn Header="Data2" Binding="{Binding Path=MyItemValue}" Width="*"&gt;DataGridTextColumn&gt;
            DataGrid.Columns&gt;
        DataGrid&gt;
    Grid&gt;
Window&gt;

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDataGrid1
{
    ///

    /// Interaction logic for MainWindow.xaml
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List&lt;MyItem&gt; MyItems = new List&lt;MyItem&gt;
                {
                    new MyItem{ MyItemName= "US", MyItemValue=77 },
                    new MyItem{ MyItemName= "US", MyItemValue=778 },
                    new MyItem{ MyItemName= "US", MyItemValue=78 },
                    new MyItem{ MyItemName= "UK", MyItemValue=7 },
                    new MyItem{ MyItemName= "Israel", MyItemValue=9 },
                    new MyItem{ MyItemName= "Turkey", MyItemValue=98 },
                    new MyItem{ MyItemName= "Turkey", MyItemValue=4 }
                };

            CollectionViewSource collection = new CollectionViewSource();
            collection.Source = MyItems;
            PropertyGroupDescription propertyGroupDescription = new PropertyGroupDescription("MyItemName");
            collection.GroupDescriptions.Add(propertyGroupDescription);
            dataGrid1.ItemsSource = collection.View;
        }
    }
}

четверг, 3 февраля 2011 г.

TFS: To make merge from stablility or build Label to trunk

For example, you have code labelled "Stable_1" in development branch and you need to merge the code to trunk.

string SourceLabel = "Stable_1"
GetStatus status = workspace.Merge
    (DevelopmentPath, TrunkPath,
    null, VersionSpec.ParseSingleSpec( "L" + SourceLabel, null ),
    LockLevel.CheckOut, RecursionType.Full, MergeOptions.None);