Monday, May 27, 2013

Set Proxy To Webbrowser in c#

private void SetProxyToBrowser(WebBrowser iWB)
        {
            try
            {
                string ProxyEmail = "" ;
                string ProxyPass = "" ;
                string ProxyUID = "" ;
                string ProxyUPass = "" ;
                string ProxyIp = "" ;
                int ProxyPort = 0;

                ProxyIp = "Proxy IP";
                ProxyPort = Proxy Port;
                ProxyUID = "Proxy User ID"; // IF You Have
                ProxyUPass = "Proxy User Password"; // IF You Have
                ProxyEmail = "Proxy Email ID";
                ProxyPass = "Proxy Email Password";

                Application.DoEvents();

                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest .Create(SignInAddress);

                WebProxy myProxy = new WebProxy(ProxyIp, ProxyPort);
                if (ProxyUID.Trim() != "" && ProxyUPass.Trim() != "")
                    myProxy.Credentials = new NetworkCredential (ProxyUID, ProxyUPass);

                myRequest.Proxy = myProxy;

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                iWB.DocumentStream = myResponse.GetResponseStream();
            }
            catch
            {
                //Code...
            }

        }

Friday, May 24, 2013

How To Delete All Files And Folder

Using System.IO;

public static void CleanFiles(String FolderPath)
     {
            if (Directory.Exists(FolderPath))
            {
                var directory = new DirectoryInfo(FolderPath);
               foreach (FileInfo file in directory.GetFiles())
               { 
                  if(!IsFileLocked(file)) 
                       file.Delete(); 
               }
               foreach (DirectoryInfo subfolder in directory.GetDirectories())
                {
                    CleanFiles(subfolder);
                }
            }
     }

public static Boolean IsFileLocked(FileInfo file)
        {
            FileStream stream = null;
 
            try
            {
                //Don't change FileAccess to ReadWrite, 
                //because if a file is in readOnly, it fails.
                stream = file.Open
                (
                    FileMode.Open, 
                    FileAccess.Read, 
                    FileShare.None
                );
            }
            catch (IOException)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
 
            //file is not locked
            return false;
        }

How To check File Is Being Another Process ,How To Check File Lock,How To CHeck File is In Used In another Proces

Using System.IO;
public static Boolean IsFileLocked(FileInfo file)
        {
            FileStream stream = null;
 
            try
            {
                //Don't change FileAccess to ReadWrite, 
                //because if a file is in readOnly, it fails.
                stream = file.Open
                (
                    FileMode.Open, 
                    FileAccess.Read, 
                    FileShare.None
                );
            }
            catch (IOException)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
 
            //file is not locked
            return false;
        }

Saturday, May 11, 2013

How To Zip And Unzip Files And Folder Fast,compressed Files And Folder Fast with ICSharpCode.SharpZipLib.Zip


///////////Download This Dll From Link
///////////http://sourceforge.net/projects/sharpdevelop/files/SharpZipLib/0.86/SharpZipLib_0860_Bin.zip/dow/nload?use_mirror=kaz
///////////and Add This Dll in project



using ICSharpCode.SharpZipLib.Zip;
//Zip a Folder
public static void ZipToFilder(string ZipFolderPath, string SavePath)
        {
            StringBuilder newPath = new StringBuilder(SavePath);
            ZipInputStream zipIn = new ZipInputStream(File.OpenRead(ZipFolderPath));
            ZipEntry entry;
            if (Directory.Exists(SavePath) != true)
            {
                Directory.CreateDirectory(SavePath);
            }
            while ((entry = zipIn.GetNextEntry()) != null)
            {
                if (entry.Name.EndsWith("/"))
                {
                    Directory.CreateDirectory(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                }
                else
                {
                    FileStream streamWriter = File.Create(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                    long size = entry.Size;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = zipIn.Read(data, 0, data.Length);
                        if (size > 0) streamWriter.Write(data, 0, (int)size);
                        else break;
                    }
                    streamWriter.Close();
                    streamWriter.Dispose();
                }
            }
            zipIn.Close();
        }

//UnZip a Folder
        public static void FolderToZip(string sTargetFolderPath, string ZipFileName)
        {
            string sZipFileName = ZipFileName;
            string[] filenames = Directory.GetFiles(sTargetFolderPath);
            //string ZipPath = Directory.GetParent(sTargetFolderPath).ToString();
            string ZipPath = sTargetFolderPath;


            using (ZipOutputStream s = new ZipOutputStream(File.Create(ZipPath + "\\" + sZipFileName + "")))
            {
                s.SetLevel(9); // 0-9, 9 being the highest level of compression
                byte[] buffer = new byte[4096];
                foreach (string file in filenames)
                {
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);

                        } while (sourceBytes > 0);
                    }
                }
                s.Finish();
                s.Close();
            }
            //Directory.Delete(sTargetFolderPath + "\\TempZipFile\\", true);
        }

Friday, May 3, 2013

strip string ,remove text char from string,get only text string from full string,remove charackter from string


 private string StripString(string StrText)
        {
            string StripText = "";
            foreach (char chritem in StrText.ToCharArray())
            {
                if (char.IsDigit(chritem) != false)
                {
                    StripText += chritem;
                }
            }
            return StripText;
        }

get only text string from string ,strip number,remove number from string,get only text string from string


private string StripNumber(string StrText)
        {
            string StripText = "";
            foreach (char chritem in StrText.ToCharArray())
            {
                if (char.IsDigit(chritem) == false)
                {
                    StripText += chritem;
                }
            }
            return StripText;
        }

Thursday, May 2, 2013

Getting Schema Information from the Data Reader


static void GetSchemaInfo(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();
        DataTable schemaTable = reader.GetSchemaTable();

        foreach (DataRow row in schemaTable.Rows)
        {
            foreach (DataColumn column in schemaTable.Columns)
            {
                Console.WriteLine(String.Format("{0} = {1}",
                   column.ColumnName, row[column]));
            }
        }
    }
}

Get multiple results from data reader c#, retrieving data using data reader


static void RetrieveMultipleResults(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
          "SELECT EmployeeID, LastName FROM dbo.Employees",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.HasRows)
        {
            Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
                reader.GetName(1));

            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
            reader.NextResult();
        }
    }
}