Musings

A random collection

Archive for March 2010

TECH: SQL Server Backups

backup database [DatabaseName] to disk = 'c:\a1.bak' with noformat, noinit, name = 'Some name for the backup', norewind, nounload, skip
Advertisements

Written by curious

March 25, 2010 at 2:13 pm

Posted in databases

.NET: Data Binding for an int

    public class CurrentPrice
    {
        private double m_val;

        public event EventHandler ValueChanged;

        public double Value
        {
            get { return m_val; }

            set {
                if (m_val != value)
                {
                    m_val = value;
                    if (ValueChanged != null)
                    {
                        ValueChanged(this, EventArgs.Empty);
                    }
                }
            }
        }
    }
CurrentPrice my_price = new CurrentPrice();
Binding price_binding = new Binding("Text", my_price, "Value");
price_binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
price_binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
currentPriceLabel.DataBindings.Add(price_binding);

Written by curious

March 23, 2010 at 3:57 pm

Posted in dotNET

.NET: DataGridView slow paint

To speed up painting of DataGridView:

// drop this class anywhere in your project
public static class ExtensionMethods
{
    public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
            BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, null);
    }
}

// then you can do the following
dataGridView1.DoubleBuffered(true);

Source: http://bitmatic.com/csharp/fixing-a-slow-scrolling-datagridview

Another alternative is:

    public class BufferedGridView : DataGridView
    {
        public BufferedGridView()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            // dgv.SetStyle(ControlStyles.Opaque, true);
        }
    }

    // then instead of DataGridView, use BufferedGridView class

Written by curious

March 23, 2010 at 9:41 am

Posted in dotNET

TECH: Accelerated Downloads on Linux

With Axel or lftp

axel -n 3 http://url/...

lftp -e 'pget -n 5 http://url/...'

Written by curious

March 16, 2010 at 11:41 am

Posted in tech-tips

Protected: QUANT: Hedge Funds

This content is password protected. To view it please enter your password below:

Written by curious

March 16, 2010 at 9:45 am

Posted in quant-finance

.NET: File I/O

Reading an environment variable

string winDir = System.Environment.GetEnvironmentVariable("windir");

Reading a file

public void read_a_file()
{
    StreamReader reader = new StreamReader(@"c:\temp\hola.txt");
    try
    {
        do
        {
            Console.WriteLine(reader.ReadLine());
        }
        while (reader.Peek() != -1);
    }
    catch
    {
        System.Console.WriteLine("File is empty");
    }
    finally
    {
        reader.Close();
    }
}

Writing to a file

public void write_to_file()
{
    StreamWriter writer = new StreamWriter(@"c:\temp\aloha.txt");
    writer.WriteLine("A file created by c-sharp code");
    writer.Close();
}

Locking a file

System.IO.FileStream fs =
    new System.IO.FileStream(
        filename,
        System.IO.FileMode.OpenOrCreate,
        System.IO.FileAccess.ReadWrite,     // both read+write
        System.IO.FileShare.None
        );

Opening a file for both Read and Write

System.IO.FileStream fs =
    new System.IO.FileStream(
        filename,
        System.IO.FileMode.OpenOrCreate,
        System.IO.FileAccess.ReadWrite,     // both read+write
        System.IO.FileShare.None               // locked mode
        );
System.IO.StreamReader inFile = new System.IO.StreamReader(fs);
System.IO.StreamWriter outFile = new System.IO.StreamWriter(fs);

// do reads on inFile
string line = inFile.ReadLine();

// do writes on outFile
outFile.WriteLine("Bla bla bla");

Reading last line of a file

Very dirty/inefficient way:

System.IO.StreamReader inFile = new System.IO.StreamReader(fs);
string str = inFile.ReadToEnd();
int x = str.LastIndexOf('\n', s2.Length - 2);
string lastLine = s2.Substring(x + 1);

Writing at the end of a file (i.e. append to a file)

System.IO.FileStream fs = new System.IO.FileStream(
        filename,
        System.IO.FileMode.OpenOrCreate,
        System.IO.FileAccess.ReadWrite,
        System.IO.FileShare.None
    );
fs.Seek(0, System.IO.SeekOrigin.End);
System.IO.StreamWriter outFile = new System.IO.StreamWriter(fs);
outFile.WriteLine("Bla bla at the end");

Print File Information

public void print_file_info(string f)
{
    FileInfo file_info = new FileInfo(f);
    System.Console.WriteLine("File Name = " + file_info.FullName);
    System.Console.WriteLine("Creation Time = " + file_info.CreationTime);
    System.Console.WriteLine("Last Access Time = " + file_info.LastAccessTime);
    System.Console.WriteLine("Last Write TIme = " + file_info.LastWriteTime);
    System.Console.WriteLine("Size = " + file_info.Length);
}

List Logical Drives

public void list_drives()
{
    string[] drives = Directory.GetLogicalDrives();
    foreach (string drive in drives)
    {
        Console.WriteLine("Drive: " + drive);
    }
}

List All Subdirectories

public void list_dir()
{
    string parent = @"c:\temp";
    string[] dirs = Directory.GetDirectories(parent);
    foreach (string dir in dirs)
    {
        Console.WriteLine("Dir: " + dir);
    }
}

List Files in a directory

public void list_files()
{
    string parent = @"c:\temp";
    string[] files = Directory.GetFiles(parent);
    foreach (string i in files)
    {
        Console.WriteLine("File: " + i);
    }
}

Written by curious

March 10, 2010 at 7:27 pm

Posted in dotNET

INDIA: ॐ जय जगदीश हरे

ॐ जय जगदीश हरे | स्वामी जय जगदीश हरे |
भक्त जनो के संकट | दास जनो के संकट | क्षण में दूर करे ||
ॐ जय जगदीश हरे

जो ध्यावे फल पावे | दुःख बिनसे मन का | स्वामी दुःख बिनसे मन का |
सुख सम्पति घर आवे | सुख सम्पति घर आवे | कष्ट मिटे तन का |
ॐ जय जगदीश हरे

मात पिता तुम मेरे | शरण गहूँ मैं किसकी | स्वामी शरण गहूँ मैं किसकी |
तुम बिन और ना दूजा | तुम बिन और ना दूजा | आस करूँ मैं जिसकी |
ॐ जय जगदीश हरे ||

तुम पूरण परमात्मा | तुम अंतर्यामी | स्वामी तुम अंतर्यामी |
पारब्रह्म परमेश्वर | पारब्रह्म परमेश्वर | तुम सबके स्वामी |
ॐ जय जगदीश हरे ||

तुम करुणा के सागर | तुम पालन करता | स्वामी तुम पालन करता |
मैं मूरख खल कामी | मैं सेवक तुम स्वामी | कृपा करो भरता |
ॐ जय जगदीश हरे ||

तुम हो एक अगोचर | सबके प्राण पति | स्वामी सबके प्राण पति |
किस विधि मिलूं दयामय | किस विधि मिलूं दयामय | तुमको मैं कुमति |
ॐ जय जगदीश हरे ||

दीन बंधु दुख हर्ता | ठाकुर तुम मेरे | स्वामी ठाकुर तुम मेरे |
अपने हाथ बढाओ/उठाओ | अपनी शरण लगाओ | द्वार पड़ा तेरे |
ॐ जय जगदीश हरे ||

विषय विकार मिटाओ | पाप हरो देवा | स्वामी पाप हरो देवा |
श्रधा भक्ति बढाओ | श्रधा भक्ति बढाओ | संतन की सेवा |
ॐ जय जगदीश हरे ||

Written by curious

March 6, 2010 at 8:47 pm

Posted in mantras