Wednesday, January 30, 2013

C# Create Delete Move Copy Rename Folder

Folder Manipulation (Create Delete Move Copy Rename Folder) in C# Sample Code:
 

//include this
using System.IO;

public void CreateFolder(string FolderPath)
{
    // Specify the directories you want to manipulate.
    DirectoryInfo di = new DirectoryInfo(FolderPath);
    try
    {
        //check folder already exists.
        if (di.Exists)
        {
            MessageBox.Show("Folder already exists.");

        }
        else 
        {
            //create folder
            di.Create();
        }  
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

public void DeleteFolder(string FolderPath)
{
    // Specify the directories you want to manipulate.
    DirectoryInfo di = new DirectoryInfo(FolderPath);
    try
    {
        //check folder exists.
        if (di.Exists)
        {
            //delete all including sub folder
            di.Delete(true);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    if (source.FullName.ToLower() == target.FullName.ToLower())
    {
        return;
    }

    // check target folder exists 
    if (Directory.Exists(target.FullName) == false)
    {
        //create target target folder
        Directory.CreateDirectory(target.FullName);
    }

    //copy each file to target folder
    foreach (FileInfo fi in source.GetFiles())
    {
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }

    //copy each sub folder to target folder
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
        //copy all file from subfolder to target subfolder
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

public void MoveAll(DirectoryInfo source, DirectoryInfo target)
{
    try
    {
        //check source folder exist
        if(source.Exists)
            //check destination not exist
            if(!target.Exists)
                source.MoveTo(target.ToString());
    }
    catch (IOException e)
    {
        MessageBox.Show(e.Message);
    }
}

public void RenameTo(DirectoryInfo di, string NewName)
{
    if (di == null)
    {
        MessageBox.Show("Directory info to rename cannot be null");
    }

    if (string.IsNullOrWhiteSpace(NewName))
    {
        MessageBox.Show("New name cannot be null or blank");
    }

    di.MoveTo(Path.Combine(di.Parent.FullName, NewName));
}

//method usage
CreateFolder(@"C:\NewFolder");
DeleteFolder(@"C:\FolderToDelete");

DirectoryInfo Source = new DirectoryInfo(@"C:\SourceFolder");
DirectoryInfo Target = new DirectoryInfo(@"C:\TargetFolder");
CopyAll(Source, Target);
MoveAll(Source, Target);

RenameTo(Source, "NewName");


Additional Reading :
  1. MSDN Directory Info
  2. Move Folder Example
  3. Renaming Folder Example

C# File in Directory List

To Get List of File in directory. Sample Code:
 
//include this
using System.IO;

//set directory to scan
var dirInfo = new DirectoryInfo(@"C:\FolderName");

//checking if folder exists
if (dirInfo.Exists) {
    //*.* = get all file name
    FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly);   

    foreach (FileInfo file in files) {
        //example to list the file name using messagebox
        MessageBox.Show(file.ToString());
    }
}
Additional Reading at :
  1. MSDN DirectoryInfo
  2. MSDN GetFiles

Tuesday, January 1, 2013

C# Debug.Writeline

Debug.WriteLine is equivalent to Debug.Print in VB. Sample Code:
 

//include this
using System.Diagnostics;

//example
String Text = "Text Something";
Debug.WriteLine(Text);

C# ODP.NET Without Oracle Client

Required File:

where to download:
Oracle Website ODP.NET


How To Use:
 
//add reference Oracle.DataAccess to project

//include Oracle.DataAccess
using Oracle.DataAccess.Client;

//create connection string
string OraconnectionString = "user id=" + USERID + ";password=" + USERPASSWORD + ";" +
                                "data source=(DESCRIPTION=(ADDRESS=" +
                                "(PROTOCOL=tcp)(HOST=" + IPorSERVERNAME + ")" +
                                "(PORT=1521))(CONNECT_DATA=" +
                                "(SERVICE_NAME=" + ORACLESID + ")))";
//example Connection to DB
OracleConnection OraConnection = new OracleConnection(OraconnectionString);

try
{
    OraConnection.Open();
}
catch (OracleException err)
{
    OraConnection.Close();
}
finally
{
    OraConnection.Close();
}


//example insert/update/delete
OracleCommand OraCommand = new OracleCommand("", OraConnection);

if (OraConnection.State.ToString() == "Open")
{
    try
    {
        //sql command
        OraCommand.CommandText =
                "insert into " + table + " (" + field + ") values (" + data + ")";

        OraCommand.ExecuteNonQuery();

        OraCommand = null;

    }
    catch (OracleException ex)
    {
        return ex.Message.ToString();
    }
}
else
{
    return "Oracle Connection Close";
}


//example SQL select statement
string strSQL = "select COLUMN_NAME from TABLE";

//assign connection and sql statement
OracleCommand OraCommand2 = new OracleCommand(strSQL, OraConnection);
//create data reader
OracleDataReader OraDataReader = null;

if (OraConnection.State.ToString() == "Open")
{
    try
    {
        //execute data reader
        OraDataReader = OraCommand2.ExecuteReader();

        //read SQL result
        while (OraDataReader.Read() == true)
        {
            OraDataReader["COLUMN_NAME"].ToString();
        }

        OraDataReader.Close();

    }
    catch (OracleException ex)
    {
        ex.Message.ToString();
    }
}
else
{
    return "Oracle Connection Close";
}