Thursday, March 27, 2014

Make Corner Curve in Win C#

I have taken reference from below url for this code
http://stackoverflow.com/questions/10674228/form-with-rounded-borders-in-c


Code:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
-----
    public partial class Form1 : Form
    {
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
         );

        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
        }
    }

Tuesday, March 25, 2014

Check Application Installed in C#

Use the Following function to check the application is installed in User System or not. This code reference is taken from below link :
http://stackoverflow.com/questions/16379143/check-if-application-is-installed-in-registry


public static bool checkInstalled (string c_name)
{
            string displayName;

            string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
            if (key != null)
            {
                foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
                {
                    displayName = subkey.GetValue("DisplayName") as string;

                    if (displayName != null && displayName.Contains(c_name))
                    {
                        return true;
                    }
                }
                key.Close();
            }

            registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
            key = Registry.LocalMachine.OpenSubKey(registryKey);
            if (key != null)
            {
                foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
                {
                    displayName = subkey.GetValue("DisplayName") as string;

                    if (displayName != null && displayName.Contains(c_name))
                    {
                        return true;
                    }
                }
                key.Close();
            }
            return false;

}

and Use this as follow

if(checkInstalled("Application Name"))

Attach external file in excel sheet by C#

Just add file in your worksheet by following code

ws.Shapes.AddOLEObject(Filename: @"c:\data\filename.pdf", Height: 10, Width: 10, Top: 150, Left: 300);

Wednesday, March 19, 2014

Write Excel from C#

I am creating Excel sheet from ListViewItem Collection i.e. from System.Windows.Forms.ListView.ListViewItemCollection, You can take any array for this.

So here is my code to generate the excel sheet

public static void createExcel(System.Windows.Forms.ListView.ListViewItemCollection items)
{
     //Creating Excel Sheet for Report
     Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
     if (xlApp == null)
     {
         Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
         return;
     }
     xlApp.Visible = true;

     Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
     Worksheet ws1 = (Worksheet)wb.Worksheets[1];
     ws1.Name = "Failed Result";
     Worksheet ws2 = (Worksheet)wb.Worksheets.Add();
     ws2.Name = "Success Result";

     if (ws1 == null || ws2 == null)
     {
         Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
     }

     int success = 0;
     int failed = 0;
     for (int i = 1; i <= items.Count; i++)
     {
         // Fill the cells in the selected range of the worksheet with the number 6.
         if (items[i - 1].BackColor == Color.LightSeaGreen)
         {
             success++;
             ws2.Cells[success, 1] = i.ToString();
             //This will increase the column width
             ws2.Cells[success, 2].EntireColumn.ColumnWidth = 20;
             ws2.Cells[success, 2] = items[i - 1].SubItems[1].Text.ToString();
             ws2.Cells[success, 3] = items[i - 1].SubItems[2].Text.ToString();
         }
         else
         {
             failed++;
             ws1.Cells[failed, 1] = i.ToString();
             ws1.Cells[failed, 2] = items[i - 1].SubItems[1].Text.ToString();
             ws1.Cells[failed, 3] = items[i - 1].SubItems[2].Text.ToString();
             //Link Second Sheet Cell link to First Sheet Cell
             ws1.Hyperlinks.Add(ws1.get_Range("C" + failed, Type.Missing), "#Sheet1!A"+failed, Type.Missing, "Link to Issue");
         }
     }
     DateTime date = new DateTime();
      wb.SaveAs("C:\\data\\Result"+date.ToString("g")+".xlsx");
}

Clear Safari Cache from C#

I have taken this reference from below URL, but modified some code.

http://www.catonmat.net/blog/clear-privacy-ie-firefox-opera-chrome-safari/

1. Make a .bat file and paste the following lines in that (I saved as "SafariClearCache.bat")

@echo off

set DataDir=C:\Users\%USERNAME%\AppData\Local\Applec~1\Safari
set DataDir2=C:\Users\%USERNAME%\AppData\Roaming\Applec~1\Safari

del /q /s /f "%DataDir%\History"
rd /s /q "%DataDir%\History"

del /q /s /f "%DataDir%\Cache.db"
del /q /s /f "%DataDir%\WebpageIcons.db"

del /q /s /f "%DataDir2%"
rd /s /q "%DataDir2%"

2. Now in your C# code include

using System.Diagnostics;

3. Now add following code of C#

public static void ClearSafariCache()
{
   Process proc = null;
   try
   {
       proc = new Process();
       proc.StartInfo.FileName = "SafariClearCache.bat";
       proc.StartInfo.CreateNoWindow = false;
       proc.Start();
       proc.WaitForExit();
   }
   catch (Exception ex)
   {
       Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
   }

}

Clear FireFox Cache from C#

I have taken this reference from below URL, but modified some code.

http://www.catonmat.net/blog/clear-privacy-ie-firefox-opera-chrome-safari/

1. Make a .bat file and paste the following lines in that (I saved as "FireFoxClearCache.bat")

@echo off

set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles

del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"

for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do del /q /s /f %%x\*sqlite

2. Now in your C# code include

using System.Diagnostics;

3. Now add following code of C#

public static void ClearFireFoxCache()
{
   Process proc = null;
   try
   {
       proc = new Process();
       proc.StartInfo.FileName = "FireFoxClearCache.bat";
       proc.StartInfo.CreateNoWindow = false;
       proc.Start();
       proc.WaitForExit();
   }
   catch (Exception ex)
   {
       Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
   }

}

Tuesday, March 18, 2014

Clear Chrome Cache from C#

I have taken this reference from below URL, but modified some code.

http://www.catonmat.net/blog/clear-privacy-ie-firefox-opera-chrome-safari/

1. Make a .bat file and paste the following lines in that (I saved as "ChromeClearCache.bat")

@echo off

set ChromeDir=C:\Users\%USERNAME%\appdata\Local\Google\Chrome\User Data\Default\Cache

del /q /s /f "%ChromeDir%"
rd /s /q "%ChromeDir%"

2. Now in your C# code include

using System.Diagnostics;

3. Now add following code of C#

public static void ClearChromCache()
{
   Process proc = null;
   try
   {
       proc = new Process();
       proc.StartInfo.FileName = "ChromeClearCache.bat";
       proc.StartInfo.CreateNoWindow = false;
       proc.Start();
       proc.WaitForExit();
   }
   catch (Exception ex)
   {
       Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
   }

}