Monday, July 9, 2012

File System Watcher class in .Net (C#)


Sometime you might need to monitor changes like files added, deleted, renamed for particular folder. To do so, .net provides FileSystemWatcher class to monitor such kind of activity for particular folder or file.

FileSystemWatcher class provides some events which are fired when some action done to the folder like file created, deleted, renamed etc. You can also specify filter to file system watcher if you want to watch only text file or music files etc. It also provides an option to include subdirectories to watch or monitor.

Let’s have a look on below code snippet

void FileSystemWatcherDemo_Loaded(object sender, RoutedEventArgs e)
{
    //creates an instance of FileSystemWatcher
    FileSystemWatcher watcher = new FileSystemWatcher();
    //set folder or file path to watch
    watcher.Path = @"D:\Temp\Test";
    //watches txt files only
    watcher.Filter = "*.txt";
    //enable watching subdirectories also
    watcher.IncludeSubdirectories = true;

    watcher.Created += new FileSystemEventHandler(watcher_Created);
    watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
    watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
    watcher.Changed += new FileSystemEventHandler(watcher_Changed);
           
    //starts watching/monitoring folder or file
    watcher.EnableRaisingEvents = true;
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("File changed - {0}, change type - {1}", e.Name, e.ChangeType);
}

void watcher_Renamed(object sender, RenamedEventArgs e)
{
    Console.WriteLine("File renamed - old name - {0}, new name - {1}", e.OldName, e.Name);
}

void watcher_Deleted(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("File deleted - {0}", e.Name);
}

void watcher_Created(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("File created - {0}, path - {1}", e.Name, e.FullPath);
}

Output –
File created - Text2.txt, path - D:\Temp\Test\Text2.txt
File created - Text1.txt, path - D:\Temp\Test\Text1.txt
File renamed - old name - Text2.txt, new name - Text3.txt
File deleted - Text3.txt

As per above code, I have chosen Test folder to monitor file system changes. After running this application, if I add, delete, rename any text file to this folder, it will notify FileSystemEvent to raise respective event and the code written in respective event handler will execute.

See Also - 

Wednesday, June 27, 2012

Printing Flow Document using WPF PrintDialog


In my last post I have explained how to print visual elements using PrintVisual method of PrintDialog. In this post, I will explain how we can print flow documents using Print Dialog class.

As I have explained earlier in my last post, PrintDialog class provides PrintVisual and PrintDocument methods for printing. In this post I will demonstrated how to use PrintDocument method to print Flow Document.

PrintDocument method accepts document in form of DocumentPaginator with print description. DocumentPaginator is a member of interface IDocumentPaginatorSource. To create document in WPF, majorly FlowDocument and FixedDocument classes are used. FlowDocument and FixedDocument both classes implements IDocumentPaginatorSource. So while passing FlowDocument to PrintDocument method of PrintDialog class we just need to pass DocumentPaginator of that document.

Let’s try to understand how to print FlowDocument using PrintDocument method of PrintDialog.

XAML
<Window x:Class="WpfApplication1.PrintDocumentDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Print Document Demo" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.9*" />
            <RowDefinition Height="0.2*"/>
        </Grid.RowDefinitions>
        <FlowDocumentReader Grid.Row="0">
            <FlowDocument Name="flowDocument">
                <Paragraph FontSize="20"
                           FontWeight="Bold">Printing document</Paragraph>
                <Paragraph FontSize="15">This is sample text to be printed.</Paragraph>
            </FlowDocument>
        </FlowDocumentReader>
        <Button Content="Print"
                Height="30" Width="150"
                Grid.Row="1" Click="Button_Click" />
    </Grid>
</Window>

Code
private void Button_Click(object sender, RoutedEventArgs e)
{
    PrintDialog pd = new PrintDialog();
    if (pd.ShowDialog() != true) return;

    flowDocument.PageHeight = pd.PrintableAreaHeight;
    flowDocument.PageWidth = pd.PrintableAreaWidth;

    IDocumentPaginatorSource idocument = flowDocument as IDocumentPaginatorSource;

    pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");
}

Output




As demonstrated in above code, I have written XAML code to create FlowDocument under FlowDocumentReader and added few text lines as paragraph which I wanted to print in page when user clicks on Print button.

In code, I have created an instance of PrintDialog class and set FlowDocument height and width to match with printable area of PageDialog. After that I have called PrintDocument method of PrintDialog class and passed DocumentPaginator of FlowDocument with some description.

See Also –

Monday, June 25, 2012

Printing Visuals using WPF PrintDialog

Printing is quite simpler in WPF. In this post, I will explain how to print elements using PrintDialog class of WPF.

WPF provides PrintDialog class which allows you to do printing from WPF application. PrintDialog class is available in System.Window.Controls namespace. PrintDialog provides various methods related to print and print related settings. PrintDialog provides PrintVisual and PrintDocument methods for printing. In this post I will demonstrated how to use PrintVisual method.

In WPF, Framework Elements are directly or indirectly derived from Visual class. You can read more about WPF class hierarchy here. You can print any element which derives from Visual class using PrintVisual method of PrintDialog class.

Let’s have a look on sample below.

XAML

<Window x:Class="WpfApplication1.PrintVisualDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Print Visual Demo" Height="350" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.9*" />
        <RowDefinition Height="0.2*"/>
    </Grid.RowDefinitions>
    <StackPanel Name="printPanel"
                Grid.Row="0"
                Margin="5">
        <Label Content="Sweet Baby"
            HorizontalAlignment="Center"
            FontSize="40"
            FontFamily="Calibri">
            <Label.Foreground>
                <LinearGradientBrush Opacity="1" 
                     StartPoint="0,0.5" EndPoint="1,0.5">
                    <LinearGradientBrush.GradientStops>
                        <GradientStop Color="Blue" Offset="0" />
                        <GradientStop Color="Red" Offset="0.5" />
                        <GradientStop Color="Green" Offset="1" />
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
        <Image Source="D:\Temp\baby.jpg"
                Height="150"
                Width="200">
        </Image>
    </StackPanel>
    <Button Content="Print"
                Grid.Row="1" Margin="5"
                Height="40" Width="150"
                HorizontalAlignment="Center"
                VerticalAlignment="Center" Click="Button_Click" />
</Grid>
</Window>

Code -
private void Button_Click(object sender, RoutedEventArgs e)
{
    PrintDialog pd = new PrintDialog();
    if (pd.ShowDialog() != true) return;
    pd.PrintVisual(printPanel, "Printing StackPanel...");
}

Output –






As per the example, When you click on Print button it will open print dialog. Select correct printer and hit print button, in my case I have selected PDF Creator printer which will generate PDF file. 


In code, I have just created an instance of PrintDailog class and called its ShowDialog method. And last I have called PrintVisual method from PrintDialog instance and passed Visual object which I wanted to print with descriptions. In above example I have passed StackPanel as Visual object which contains Text (Sweet Baby) and Image.

Instead of creating controls in XAML, you can dynamically create those through code and similarly print using PrintVisual method. See below code snippet.

private void Button_Click(object sender, RoutedEventArgs e)
{
    StackPanel sp = new StackPanel();

    TextBlock tb = new TextBlock();
    tb.Text = "Sweet Baby";
    tb.FontSize = 40;

    Image img = new Image();
    img.Source =new BitmapImage(new Uri(@"d:\temp\baby.jpg"));
    img.Height = 150;
    img.Width = 200;

    sp.Children.Add(tb);
    sp.Children.Add(img);

    PrintDialog pd = new PrintDialog();
    if (pd.ShowDialog() != true) return;
    pd.PrintVisual(printPanel, "Printing StackPanel...");
}

Let’s have a look on below sample which prints Canvas and elements inside.

XAML
<Window x:Class="WpfApplication1.PrintCanvasDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Print Canvas Demo" Height="400" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.9*" />
            <RowDefinition Height="0.2*"/>
        </Grid.RowDefinitions>
        <Canvas Name="printCanvas"
                Width="350" Height="280">
            <TextBlock Text="This is sample text."
                       FontSize="18"
                       FontFamily="Arial"
                       Canvas.Top="20" Canvas.Left="60"/>
            <Ellipse Height="100" Width="125"
                     Fill="LightBlue"
                     Stroke="Blue"
                     StrokeThickness="2"
                     Canvas.Right="35" Canvas.Bottom="55"/>
            <Rectangle Height="15" Width="90"
                       Fill="Orange"
                       Stroke="Red"
                       Canvas.Top="80" Canvas.Left="150" />
            <Polygon Points="140,160 80,185 50,127 130,20"
                     Stroke="Purple"
                     StrokeThickness="2"
                     Canvas.Top="90" Canvas.Left="0">
                <Polygon.Fill>
                    <SolidColorBrush Color="Blue" Opacity="0.4"/>
                </Polygon.Fill>
                <Polygon.RenderTransform>
                    <RotateTransform CenterX="0" 
                                     CenterY="250" Angle="-10" />
                </Polygon.RenderTransform>
            </Polygon>
        </Canvas>
        <Button Content="Print"
                    Grid.Row="1" Margin="5"
                    Height="40" Width="150"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center" Click="Button_Click" />
    </Grid>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true) return;
            pd.PrintVisual(printCanvas, "Printing Canvas...");
        }




Above example demonstrates how we can print Canvas and its elements. Canvas is also derived from Visual so we can print canvas using PrintVisual method of PrintDialog class. In above example instead of StackPanel I have passed Canvas as visual object.


See Also –
Transformationsin WPF

Sunday, May 13, 2012

Creating File System Task with SQL Server Integration Services (SSIS)


You can create SSIS package to perform file related operations like copy file or directory, create file or directory, delete file or directory, rename file etc. You just need to drag File System Task item from tool bar to control flow tab or simply double click on File System Task item from tool bar. Double click on File System Task item it will open editor dialog window. You can set source and destination connection properties based on selected file operation.  You can see below the list of various file operations available to perform with file system task.  



Below is the File System Task editor dialog which displays properties to be set to perform particular task. The available tasks for File System Task are displayed in above image.



As per selected operation, you need to specify source and destination connection or only specify source connection. You need to specify either existing connection or create new connection. Below is the new file connection dialog.



You can select usage type as create file, create folder, existing file or existing folder. Specify file name with folder location. After setting all the parameters, you can execute package and your selected file operation successfully completed.

Below is the video which explains how to create and execute file system task with different file operations.




See Also –