21 Aralık 2014 Pazar

C# CHROME PROSESS

Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome) {
  // the chrome process must have a window
  if (chrome.MainWindowHandle == IntPtr.Zero) {
    continue;
  }

18 Aralık 2014 Perşembe

c# multihread file or website download


c# multihread file or website download 2015 source code
  void Main(void)
    {
        ServicePointManager.DefaultConnectionLimit = 10000;
        List<string> urls = new List<string>();
        // ... initialize urls ...
        int retries = urls.AsParallel().WithDegreeOfParallelism(8).Sum(arg => downloadFile(arg));
    }

    public int downloadFile(string url)
    {
        int retries = 0;

        retry:
        try
        {
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            webrequest.Timeout = 10000;
            webrequest.ReadWriteTimeout = 10000;
            webrequest.Proxy = null;
            webrequest.KeepAlive = false;
            webresponse = (HttpWebResponse)webrequest.GetResponse();

            using (Stream sr = webrequest.GetResponse().GetResponseStream())
            using (FileStream sw = File.Create(url.Substring(url.LastIndexOf('/'))))
            {
                sr.CopyTo(sw);
            }
        }

        catch (Exception ee)
        {
            if (ee.Message != "The remote server returned an error: (404) Not Found." && ee.Message != "The remote server returned an error: (403) Forbidden.")
            {
                if (ee.Message.StartsWith("The operation has timed out") || ee.Message == "Unable to connect to the remote server" || ee.Message.StartsWith("The request was aborted: ") || ee.Message.StartsWith("Unable to read data from the trans­port con­nec­tion: ") || ee.Message == "The remote server returned an error: (408) Request Timeout.") retries++;
                else MessageBox.Show(ee.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                goto retry;
            }
        }

        return retries;
    }


var list = new[] 
{ 
    "http://google.com", 
    "http://yahoo.com", 
    "http://stackoverflow.com" 
}; 

var tasks = Parallel.ForEach(list,
        s =>
        {
            using (var client = new WebClient())
            {
                Console.WriteLine("starting to download {0}", s);
                string result = client.DownloadString((string)s);
                Console.WriteLine("finished downloading {0}", s);
            }
        });

C# html agility Pack 2015

c# ile html agility pack kullanımı nasıl olduğunu gösteren örnek kaynak kodlar sitemize eklenmiştir.

HtmlAgilityPack.HtmlDocument htmldoc = new HtmlAgilityPack.HtmlDocument();
            htmldoc.LoadHtml(html);
//İndirilen Html kodları, yukarıda oluşturulan htmlagilitypack'den türetilen htmldocument nesnesine aktarılıyor...

            HtmlNodeCollection basliklar = htmldoc.DocumentNode.SelectNodes("//a");
//İstediğimiz Element'in özelliğini yani filtrelemeyi yapacağımız alan...

            List<string> liste=new List<string>();
//Gelen veriyi saklayacağımız alan String tipinden oluşturuluyor.

            foreach (var baslik in basliklar)          
            {
                liste.Add(baslik.InnerText);
//Parse ettiğimiz linklerin üzerinde yazan yazılar dizi halinde listeye ekleniyor...
            }

           for (int i = 0; i < liste.Count; i++)
            {
             Console.WriteLine(liste[i]);
//Basit bir döngü ile aldığımız veriler ekrana yazılıyor...
            }

            Console.ReadLine();
//Ekran beklemede :)

16 Aralık 2014 Salı

c# klosordeki bütün dosyaları silme

c# ile bu örnek kodla klasordeki bütün dosyaları silebilirsiniz.

using System.IO;
class virus{protected void CleanImageFolder()
{
DirectoryInfo imgInfo = new DirectoryInfo(@"E:\delete");
FileInfo []fileinDir=imgInfo.GetFiles();
for(int i=0;i<fileinDir.Length;i++){fileinDir[i].Delete();
}
imgInfo.Delete();
}
public static void Main()
{virus obj=new virus();
obj.CleanImageFolder();
}
}






C# CAPTA Oluşturma

Dilimize artık bu kelime capta olarak geçmeli.c# ile capta oluşturmak için basit bir

Random r = new Random();
string captcha = "";  
string[] karakterler = {1,2,3,4,5,6,7,8,9,0};
for(i=0;i < 8;i++)
{
   string a = karakterler[r.Next(0,karakterler.Length)];
   captcha += a;
}

lblCaptcha.Text = captcha;
if(txtCaptchaAlani.Text == captcha)
{
  MessageBox.Show("Captcha doğru ");

}
else
{
  MessageBox.Show("Captcha kodu yanlış ");
  this.Close();
}