writing &reading windows registry in c# .net code

Write Registry Entry code

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("names");
key.SetValue("pankaj Sharma", "Developer");
key.Close();

Read Registry Entry Code

string s = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("names").GetValue("Pankaj Sharma").ToString();


by: Pankaj Sharma



remove cookies of webBrowser control c# .net

remove cookies of webBrowser control in c# .net


string[] Cookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
int notDeleted = 0;
foreach (string CookieFile in Cookies)
{
try
{
System.IO.
File.Delete(CookieFile);

}catch (Exception ex)

{
notDeleted++;
}

}


MessageBox.Show((Cookies.Length - notDeleted).ToString() + " Cookies Deleted, " + notDeleted.ToString() + " Cookies Not Deleted", "Cookies");



by:Pankaj Sharma

Upload File to FTP Server Using Asp.Net File Uploader


  • Using Ftp Web Request

System.Net.FtpWebRequest rq = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(@"ftp://XYZ.com/a1.temp");
rq.Credentials = new System.Net.NetworkCredential("username", "******");
rq.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
System.IO.Stream fs = FileUpload2.PostedFile.InputStream;
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
System.IO.
Stream ftpstream = rq.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ei)
{
Label1.Text = ei.Message;
}

  • Using WebClient

try


{


     string location = "ad/" + FileUpload1.FileName;
     FileUpload1.PostedFile.SaveAs(Server.MapPath(location));
     System.Net.
    WebClient wc = new System.Net.WebClient();
    wc.Credentials =     new System.Net.NetworkCredential("hello", "******");
    wc.UploadFile(    new Uri(@"ftp://xyz.com/test.no"), Server.MapPath(location));
}catch (Exception ei)
{
   Label1.Text = ei.Message;
}

     

Creating Excel file With C#

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}

convert Funtion in SQL