Monday, 27 April 2009

Run System Process without showing Command Window and get an output

In this post I would like to show the sample code on how to start a process without showing a console window and get an output result. I am going to use System.Diagnostics.Process class and set parameters to it. Function below will execute a specified command with parameters and return an output on successful run or nothing if there was an error.

using using System.Diagnostics;
...
private static string RunProcess(string command, string parameters)
{
    string output = string.Empty;

    try
    {
        Process process = new Process();
        process.StartInfo.CreateNoWindow = true;    // Don't create command window
        process.StartInfo.FileName = command;
        process.StartInfo.Arguments = parameters;
        process.StartInfo.UseShellExecute = false;  // Don't use system shell for execution 
        process.StartInfo.RedirectStandardOutput = true;    // Give us an output
        process.StartInfo.RedirectStandardError = true;     // Give us an error output
        bool r = process.Start();

        StreamReader streamReader = process.StandardOutput;
        // Read the standard output of the spawned process.
        output = streamReader.ReadLine();
        if (output == null)
        {
            StreamReader errorStream = process.StandardError;
            string error = errorStream.ReadToEnd();
        }
        process.Close();
    }
    catch
    {

    }

    return output;
}

This function can be modified to return an error output in exception or logged into file.

Wednesday, 22 April 2009

Windows Live Sync vs. Windows Live Mesh and LogMeIn.Com instant remote desktop.

Windows Live Sync vs. Windows Live Mesh

Recently I was reading several Internet articles about Windows Live Sync and Windows Live Mesh Services. Unfortunately none of these articles did not clearly described the difference between these two. Both of these services allow sync of files between multiple computers connected to the Internet and sharing these with other users, but only Live Mesh stores files in the cloud and can be used for backup and recovery. Windows Live Sync is only holds information about computers and files to sync and copies files directly from computer to computer. Files cannot be synced if one of two computer is offline. Same type of sync on the local network can be easily done using Windows SyncToy tool. The advantage of Live Sync/Mesh Service computers can be located anywhere, connected to the internet and almost instantly sync the changes to the files. Same relates to Backup. In case of unexpected disk failure backup can be restored right off the Windows Live Mesh service and will be up to date in the most cases.

Windows Live Sync allows to synchronize up to 20 folders containing up to 20,000 files each. Individual files cannot be larger than 4 GB in size.

Windows Live Mesh offers 5 GB of online storage space.

There is another service called Windows Live SkyDrive which allows to store up to 25GB with individual file not larger than 50MB. Unfortunately this service doesn't have automatic upload tool like Windows Live Mesh.

Free LogMeIn Remote Desktop Service

Free LogMeIn service allows remote control of the computer desktop connected to the internet. It does not allow file transfer or any other operations except for Remote Desktop Control of the Computer. In combination with Windows Live Mesh and Windows Live Sync Services we can have Remote Desktop as well as instant file transfer.

References

Windows Live Sync

Windows Live Mesh

Free LogMeIn Remote Desktop Service

Windows SyncToy tool

Windows Live Sync vs. Live Mesh vs. SkyDrive: Which is Right for You?

Friday, 17 April 2009

Install/Uninstall and Manage Windows Service using Service Control (sc.exe)

Service Control program is a powerful tool that can be used to Install/Uninstall and perform other actions on Windows Services. Sample screenshot shows different parameters and options for sc.exe tool.

image

I will base my example on creating SVN version control Windows Service.

To install SVN Windows Service run

sc create "Subversion Server" binpath= "path_to_subversion\bin\svnserve.exe --service --root "C:\SVNRoot""
    displayname= "Subversion Dev Repository" depend= Tcpip start= auto

To Uninstall SVN Windows Service run

sc delete "Subversion Server"

Make sure that you put space after ‘=’.

Once SVN Windows Service is created allow svnserve.exe in Windows Firewall.

References

How to create a Windows service by using Sc.exe

Setting up a Subversion Server under Windows

Wednesday, 8 April 2009

How to clone DataRow and get its auto-incremented ID number after inserting into Access Database

In my work with Access Database I needed to duplicate an existing row in the table and get an ID of newly inserted row. Below are short snippets of code showing how.

Duplicating a DataRow

// load the data into Data Table
DataTable dataTable = tableAdapter.GetData();

// clone a Data Table
DataTable duplicatedDataTable = dataTable.Clone(); 

// get row to duplicate
DataRow row = dataTable.Rows[0];    

// import row into cloned Data Table
duplicatedDataTable.ImportRow(row);  

This way we get a deep copy of the existing row and later use table adapter to insert it into database.

Getting auto-incremented ID number after inserting a row into Access Database

The actual idea of getting an auto-incremented ID of the new row in Access Database is very simple. Right after insert of the row we need to execute similar query to this one:

SELECT MAX(ID) FROM TableName
In order to avoid loading wrong ID in multi-user environment it is good to add some WHERE selection criteria which will limit selection only to duplicated rows. MAX function will pick the ID of the last inserted row.