|
Chiedi pure.
|
|
 | Invalid value |
|
|
|
|
|
 | Invalid value |
|
|
|
 | Invalid value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Effettua l'aggiornamento di un programma via FTP (C#) [C# ]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pepperminds.BusinessLogic;
using System.Net;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Diagnostics;
namespace mio_programma_update
{
public partial class Form1 : Form
{
public string ftpServerIP, ftpUserID, ftpPassword;
public int byteToRead = 0;
public int tread = 0;
public Form1()
{
InitializeComponent();
this.toolStripStatusLabel1.Text = "Pronto.";
}
public delegate void DelegateWithParameters(string param1);
private void button1_Click(object sender, EventArgs e)
{
Process[] lst = Process.GetProcesses();
bool StartingProc;
StartingProc = false;
foreach (Process p in lst)
{
if (p.ProcessName.ToLower().StartsWith("mio_programma.exe"))
StartingProc = true;
}
if (!StartingProc)
{
//this.progressBar1.Value = 0;
DelegateWithParameters mi = new DelegateWithParameters(this.Update_status);
//this.Invoke(mi);
IAsyncResult tag = mi.BeginInvoke(@"Ricerca aggiornamenti in corso.", null, null);
mi.EndInvoke(tag);
//this.toolStripStatusLabel1.Text = "Ricerca aggiornamento in corso...";
ftpServerIP = "mioFTP/Mia_cartella";
ftpUserID = @"MiaUser";
ftpPassword = @"MiaPassword";
Download(Directory.GetCurrentDirectory().ToString(), "mio_programma.exe");
mi = new DelegateWithParameters(this.Update_status);
tag = mi.BeginInvoke(@"Aggiornamento effettuato.", null, null);
mi.EndInvoke(tag);
}
else
MessageBox.Show("Impossibile eseguire l'aggiornamento se mio_programma è aperto. Chiudere mio_programma e riprovare.");
}
private void Update_status(string s)
{
this.toolStripStatusLabel1.Text = s;
}
private void Download(string filePath, string fileName)
{
FtpWebRequest reqFTP;
string t;
progressBar1.Value = 0;
try
{
//filePath: The full path where the file is to be created.
//fileName: Name of the file to be createdNeed not name on
// the FTP server. name name()
FileStream outputStream = new FileStream(filePath + "\\" +
fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
+ ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID,
ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
MessageBox.Show(cl.ToString());
tread = 0;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
t = this.toolStripStatusLabel1.Text;
while (readCount > 0)
{
tread += readCount;
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
//ChangeLabel(t + " scritti " + tread.ToString());
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
this.Invoke(mi);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateProgress()
{
if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value += 1;
}
}
}
}
|