A simple way to run C# code from bat file

I read an interested article demonstrated how to run a bat file with embedded C# code. I repost this example here, because the original post is written in Russian. Despite the fact that there are other more powerful solutions and frameworks to achieve the same goal (e.g. Powershell) – I think the way presented by the author, is very useful for different scenarios.


/*
@echo off && cls
set WinDirNet=%WinDir%\Microsoft.NET\Framework
IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
%csc% /nologo /out:"%~0.exe" %0
"%~0.exe"
del "%~0.exe"
exit
*/

class HelloWorld
{

    static void Main()
    {
        System.Console.WriteLine("Hello, World!\r\nI am at " + System.Environment.Version);
        System.Console.ReadLine();
    }
}

Sorting System.Collections.Generic.List<T>

This post shows the sorting technique using anonymous delegate that can be implemented in any custom type generic list based on the selected property.For sorting any custom type the class must implement the System.IComparable interface. Now let me assume you have a Person class and if you want to sort the list of person objects then you must implement System.IComparable interface and write the logic of how our person object is to be sorted in the CompareTo method

using System;

namespace ConsoleApplication1
{
    class Person : IComparable
    {
        public Person(string firstName, int age)
        {
            FirstName = firstName;
            Age = age;
        }

        public string FirstName
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }

        // sorting in ascending order      

        #region IComparable Members

        public int CompareTo(object obj)
        {
            var person = (Person) obj;
            return FirstName.CompareTo(person);
        }

        #endregion
    }
}

But you can simplify the sorting just by using the anonymous delegate as shown bellow and you don’t need to implement IComparable interface

var persons = new List<Person>
                    {
                        new Person("Tom", 30),
                        new Person("Harry", 55)
                    };
// sort in ascending order
persons.Sort(delegate(Person person0, Person person1)
                    {
                        return person0.FirstName.CompareTo(person1.FirstName);
                    });
// sort in descending order
persons.Sort(delegate(Person person0, Person person1)
                    {
                        return person1.FirstName.CompareTo(person0.FirstName);
                    });

Technorati tags: generic, c#, code

DataGridView Extension

DataGridView Extension is a free component that extends the standard DataGridView control, provided with .NET 2.0 Framework.

Features

  • Customization the grid in IDE (design mode support for the background color and borders,default cell formatting, alignment,colors,column and row headers formatting, column sizing …).
  • Exporting the data within the data grid in several file formats (MS Excel,Html,PDF).
  • Build-in print functionality
  • Manage multiple DataGridView controls in a single project and to store all settings for every one of them
  • The integrated security allows the developers to control what features are accessible for the end-user depending on the application’s logic.
  • Intelligent searching capabilities.
  • Set of custom column types (DateTime picker and Numeric Up/Down columns)
  • All features are programmable through the API

 

DataGridViewExtensionDemo

 

DataGridViewExtensionSettings

DataGridView Extension Downloads

 

CAKE3 – Free archiving .Net library

Cake3 library supports compressing and extracting filesĀ  to/from different archive formats .The library can be easily integrated to your projects .

Usage of Cake3 library:

Compress

Cakdir3 cdir3 = new Cakdir3(@"c:\temp\arch.lha");
cdir3.AddOptions.addFolder = AddOptions.folderMode.relative;
cdir3.AddOptions.baseFolder = @"c:\temp\";
//Mask not supported in Cake3.
cdir3.AddOptions.addFile = new String[2] {@"c:\temp\arch.zip", @"C:\temp\test\image.bmp"};
cdir3.Add();

Decompress

Cakdir3 cdir = new Cakdir3(@"c:\temp\test.lha");
cdir.ExtractOptions.extractItem = new String[1] {cdir.Archive_Contents[0].fileName};
cdir.ExtractOptions.extractFolder = Utils.GetTempPath() + "qztemp";
cdir.ExtractOptions.allowFolder = true;
cdir.ExtractOptions.allowOverwrite = true;

if (cdir.Extract())
 Console.WriteLine("Success");

Supported Archives(compress)

  • Zip archives (SharpZipLib)
  • 7z archives (7-zip32.dll)
  • lha archivesSupported Archives(extract only)
    • Zip archives (SharpZipLib)
    • 7z archives (7-zip32.dll)
    • Ace archives (acev2.dll)
    • Arj archives (Unarj32j.dll)
    • Lha archives (Unlha32.dll)
    • Sqx archives (Sqx20.dll)
    • Rar archives (unrar.dll)
    • Cab archives (Xacrett.dll)
    • Rar archives (Xacrett.dll)
    • Yz1 archives (Xacrett.dll)
    • Bga archives (Xacrett.dll)
    • Gca archives (Xacrett.dll)
    • Imp archives (Xacrett.dll)
    • Zoo archives (Xacrett.dll)
    • Arc archives (Xacrett.dll)
    • Cpt archives (Xacrett.dll)
    • Pit archives (Xacrett.dll)
    • Arg archives (Xacrett.dll)
    • Asd archives (Xacrett.dll)
    • Zac archives (Xacrett.dll)
    • MacBin archives (Xacrett.dll)
    • Tar, Cpio, Rpm archives (Xacrett.dll)
    • Shar archives (Xacrett.dll)
    • Quake Pak archives (Xacrett.dll)
    • Ar(Deb) archives (Xacrett.dll)
    • Gzip archives (Xacrett.dll)
    • Bzip2 archives (Xacrett.dll)
    • Compress archives (Xacrett.dll)
    • Pack archives (Xacrett.dll)
    • Compact archives (Xacrett.dll)
    • Freeze archives (Xacrett.dll)
    • Belon archives (Xacrett.dll)
    • Splint archives (Xacrett.dll)
    • Jam archives (Xacrett.dll)
    • MS-Compress archives (Xacrett.dll)
    • Base64 archives (Xacrett.dll)
    • UUEncoded files (Xacrett.dll)
    • XXEncoded files (Xacrett.dll)
    • BinHex archives (Xacrett.dll)
    • Ish archives (Xacrett.dll)
    • Boo archives (Xacrett.dll)
    • BtoA archives (Xacrett.dll)
    • Fish archives (Xacrett.dll)
    • Jack archives (Xacrett.dll)
    • Rez archives (Xacrett.dll)
    • Cab(InstallShield) archives (Xacrett.dll)
    • Dzip archives (Xacrett.dll)
    • yEnc archives (Xacrett.dll)
    • Dmg archives (Xacrett.dll)
    • BriefLZ archives (Xacrett.dll)
    • ISO (Xacrett.dll)
    • Alz archives (Xacrett.dll)
    • Lzx archives (Xacrett.dll)
    • Jcalgl archives (Xacrett.dll)
    • apLib archives (Xacrett.dll)
    • Neo_ASK archives (Xacrett.dll)

    Download Cake3

  • Shaped Windows in WPF

    In previous versions of Microsoft .Net Framework, creating shaped windows was a time-consuming process that involved Win API calls.Not anymore – one of the modern  aspects of WPF is that you can make  non-rectangular windows in simple way.Creating shaped forms works in the following manner.First, you create an image, which, when we are finished, will act as your window.  Second, you change a XAML to make this image become the form.

    • Create a transparent image file that will be used to set the desired shape of the window.

    shaped-form

    • Create a Windows Application(WPF) project and add the image control to it.Remember to make the window transparent as shown in code below
    <Window x:Class="WindowsApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowsApplication1" Height="300" Width="300"
        AllowsTransparency="True"
        WindowStyle="None"
        Background="Transparent" 
        >
        <Grid>
        <Image  Name="image1" />
      </Grid>
    </Window>

     

    • Add code that recreates the functionality that the title bar provided, such as moving the window and closing it.
    using System;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;
     
     
    namespace WindowsApplication1
    {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
     
            image1.Source = new BitmapImage(
     
                new Uri(@"C:\Documents and Settings\username\Desktop\Untitled.png"));
        }
     
     
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            DragMove();
        }
     
     
     
        protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonDown(e);
     
            Close();
        }
     
    }
    }
     
    Technorati Tags: WPF, XAML, dotnet

    Page 1 of 41234