Projelerin yayınlanması için setup olarak paketlenip kurulması gerekir. Ancak projelere yeni özellikler eklediğimizde her dafasında projenin kaldırılıp kurulması zaman kaybına yol açar. Halbuki bunu projenin başlangıcında kontrol edebilirsiniz. Bu işlemi gerçekleştirmek için projenin yeni versiyonlarının internet ortamında yayınlanması gerekir. Biz burada ücretsiz olarak somee.com sitesinden yararlanacağız. Projeyi oluşturalım. Aşağıdaki kodları yazalım.
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace ToplaSetup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int v = 1;
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
button1.Visible = false;
string hedef = "http://atml.somee.com/index.html";
WebRequest istek = HttpWebRequest.Create(hedef);
WebResponse yanit = istek.GetResponse();
StreamReader bilgiler = new StreamReader(yanit.GetResponseStream());
string gelen = bilgiler.ReadToEnd();
int baslangic = gelen.IndexOf("<p>") + 3;
int bitis = gelen.Substring(baslangic).IndexOf("</p");
string gelenbilgiler = gelen.Substring(baslangic, bitis);
v = Convert.ToInt16(gelenbilgiler);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (v != 1)
{
button1.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://atml.somee.com/setup.exe");
}
}
}
Bu arada web sayfamız aşağıdaki gibi olacaktır.
<html>
<head>
<title></title>
</head>
<body>
<p>
2</p>
</body>
</html>
Şimdi bu işlemi biraz daha işlevsel yapalım. Yeni proje oluşturalım. Projeye iki adet form ekleyelim. Program.cs dosyasına şu değişkeni ekleyelim.
public static string v = "1";
Form1 sayfasına aşağıdaki kodları ekleyelim.
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace ToplaSetup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int v = 1;
private void Form1_Load(object sender, EventArgs e)
{
if (kontrol())
{
DialogResult soru = MessageBox.Show("Yeni güncellemeler var.\n\rŞimdi yüklemek istermisiniz?", "Güncelleme bulundu", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (soru == DialogResult.Yes)
{
Thread t = new Thread(new ThreadStart(Basla));
t.Start();
this.Close();
}
}
}
public static void Basla()
{
Application.Run(new Form2());
}
private Boolean kontrol()
{
Boolean cevap;
try
{
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://atml.somee.com/index.html");
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
if (content == Program.v)
{
cevap = false;
}
else
{
cevap = true;
}
}
catch (Exception)
{
cevap = false;
throw;
}
return cevap;
}
}
}
Form2 sayfasına iki tane timer, 1 tane progressbar, 1 tane label ekleyelim. Timer1 nesnesinin enabled özelliğini true yapalım. Aşağıdaki kodları ekleyelim.
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
namespace ToplaSetup
{
public partial class Form2 : Form
{
private string path = Application.StartupPath + "\\setup.exe";
public Form2()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://atml.somee.com/setup.exe"), path);
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label1.Text = "İlerleme durumu : indirme tamamlandı.";
timer2.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(path);
this.Close();
}
}
}
Projeyi kaydedip çalıştıralım. Sonucu görelim.
|