I have this C# code that does not do the job. This code should run a code when the download is complete. But it does not work.
Code:
private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
private void flatToggle2_CheckedChanged(object sender)
{
if (flatToggle2.Checked)
{
//Create File
Directory.CreateDirectory("web/data/main/");
timer2.Start();
bunifuProgressBar1.Show();
bunifuCustomLabel1.Show();
//Downloand
using (var client = new WebClient())
{
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
}
}
else
{
}
}
This is the code that does not work
private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
I tried to run this code without the Unzip but it did not work so I need help with this code.
Looking at the documentation, DownloadFile() is synchronous, so there is no callback needed. Try instead just:
using (var client = new WebClient())
{
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
This should suffice.
Related
I have a small problem with downloading a file.
I have used windows Forms and need to download several files. They all are avaiable through a link that, when its opened automatically downloads the file.
I tried several things like WebCient or httpWebrequest but it all doesnt work.
Do you have any idea how i can launch this link without opening the browser every time and safe the file to a specific folder.
foreach (var doc in newDocs)
{
using (var wb = new WebClient())
{
MessageBox.Show("link" <- to cehck the if its the correct file;
HttpWebRequest request = WebRequest.Create("link") as HttpWebRequest;
wb.DownloadFile("link" <- 'link' cause its sensitive data.);
}
}
Hope it will help you.
void Download()
{
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadFileAsync(new Uri("url"), #"filepath");
}
}
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//throw new NotImplementedException();
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//throw new NotImplementedException();
}
I want to achieve same functionality as like this one :
Notify C# Client, when SMTP Server receive a new Email
This is working fine. That method "NewMessageReceived" call successfully on every new mail receive.
But problem is that i can't fetch mail. I only got total count of INBOX in this method.
This code using the library : MailSystem.net
My Code as following :
Imap4Client _imap;
public static int cnt = 0;
protected void Application_Start(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(StartIdleProcess);
if (worker.IsBusy)
worker.CancelAsync();
worker.RunWorkerAsync();
}
private void StartIdleProcess(object sender, DoWorkEventArgs e)
{
if (_imap != null && _imap.IsConnected)
{
_imap.StopIdle();
_imap.Disconnect();
}
_imap = new Imap4Client();
_imap.ConnectSsl(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]));
_imap.Login(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);
var inbox = _imap.SelectMailbox("INBOX");
_imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);
inbox.Subscribe();
_imap.StartIdle();
}
public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
{
cnt = e.MessageCount; //Inbox Count
//here i want to fetch all unread mail from inbox
}
I have found following solution to fetch unread mail:
http://mailsystem.codeplex.com/discussions/651129
It is saying me to use MailKit library. As i am already using MailSystem.net library , is there any way to achieve my requirement by using only one library ?
I am ready to use any other library also.
Please give me suggestion. Thanks
Yes, got it with single library : MailKit
Look at following code of global.asax file:
static CancellationTokenSource _done;
ImapClient _imap;
protected void Application_Start(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(StartIdleProcess);
if (worker.IsBusy)
worker.CancelAsync();
worker.RunWorkerAsync();
}
private void StartIdleProcess(object sender, DoWorkEventArgs e)
{
_imap = new ImapClient();
_imap.Connect(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]), Convert.ToBoolean(ConfigurationManager.AppSettings["IncomingIsSSL"]));
_imap.AuthenticationMechanisms.Remove("XOAUTH");
_imap.Authenticate(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);
_imap.Inbox.Open(FolderAccess.ReadWrite);
_imap.Inbox.MessagesArrived += Inbox_MessagesArrived;
_done = new CancellationTokenSource();
_imap.Idle(_done.Token);
}
static void Inbox_MessagesArrived(object sender, EventArgs e)
{
var folder = (ImapFolder)sender;
//_done.Cancel(); // Stop idle process
using (var client = new ImapClient())
{
client.Connect(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]), Convert.ToBoolean(ConfigurationManager.AppSettings["IncomingIsSSL"]));
// disable OAuth2 authentication unless you are actually using an access_token
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);
int tmpcnt = 0;
client.Inbox.Open(FolderAccess.ReadWrite);
foreach (var uid in client.Inbox.Search(SearchQuery.NotSeen))
{
try
{
var message = client.Inbox.GetMessage(uid);
client.Inbox.SetFlags(uid, MessageFlags.Seen, true);
List<byte[]> listAttachment = new List<byte[]>();
if (message.Attachments.Count() > 0)
{
foreach (var objAttach in message.Attachments)
{
using (MemoryStream ms = new MemoryStream())
{
((MimeKit.ContentObject)(((MimeKit.MimePart)(objAttach)).ContentObject)).Stream.CopyTo(ms);
byte[] objByte = ms.ToArray();
listAttachment.Add(objByte);
}
}
}
string subject = message.Subject;
string text = message.TextBody;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<myHub>();
hubContext.Clients.All.modify("fromMail", text);
tmpcnt++;
}
catch (Exception)
{ }
}
client.Disconnect(true);
}
}
Now, i am able to receive new mail arrival event and also able to fetch unread mail in that event. Thanks MailKit.
I've looked in many places for this but still haven't found a solution. What I'm trying to achieve is being able to use BackgroundWorker on a timed basis. Here's an example:
public Main()
{
isDbAvail = new BackgroundWorker();
isDbAvail.DoWork += isOnline;
isDbAvail.RunWorkerCompleted += rewriteOnlineStatus;
}
private void rewriteOnlineStatus(object sender, RunWorkerCompletedEventArgs e)
{
Subs.Connection connection = new Subs.Connection();
changeStatus(connection.isDbAvail());
}
private void isOnline(object sender, DoWorkEventArgs e)
{
while (true)
{
Console.WriteLine("Checking database connection");
System.Threading.Thread.Sleep(8000);
}
}
public void changeStatus(bool status)
{
if (status)
{
serverStatusVal.Text = "Connected";
serverStatusVal.ForeColor = System.Drawing.Color.DarkGreen;
}
else
{
serverStatusVal.Text = "Not connected";
serverStatusVal.ForeColor = System.Drawing.Color.Red;
}
}
What's happening here is that the isOnline method checks if there is a connection to the database (just an example) every 8 seconds and changes the text accordingly. What I've noticed though, is that the while loop inside the isOnline method causes the rewriteOnlineStatus method never to fire because it runs indefinitely. Is there another workaround to this?
I suggest you use BackgroundWorker.ReportProgress, and check connectivity in the background thread.
Something like this:
public Main()
{
isDbAvail = new BackgroundWorker();
isDbAvail.WorkerReportsProgress = true;
isDbAvail.DoWork += isOnline;
isDbAvail.ProgressChanged += rewriteOnlineStatus;
isDbAvail.RunWorkerAsync();
}
private void rewriteOnlineStatus(object sender, ProgressChangedEventArgs e)
{
changeStatus((bool)e.UserState);
}
private void isOnline(object sender, DoWorkEventArgs e)
{
while (true)
{
Console.WriteLine("Checking database connection");
Subs.Connection connection = new Subs.Connection();
isDbAvail.ReportProgress(0, connection.isDbAvail);
System.Threading.Thread.Sleep(8000);
}
}
Now the BackgroundWorker is doing the work, and reporting back to the UI thread via ProgressChanged.
Well, Im trying to donwload a file from GitHub using WebClient C# Class but I always get the file corrupted.. this is my code
using (var client = new WebClient())
{
client.DownloadFile("https://github.com/trapped/rotmg_svr/archive/master.zip", #"C:/Users/Asus/Desktop/aa.zip");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
}
static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage.ToString());
}
//////
public static void ReadFile()
{
WebClient client = new WebClient();
client.DownloadFile("https://github.com/trapped/rotmg_svr/archive/master.zip", #"C:/Users/Asus/Desktop/aa.zip");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
}
static void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Console.WriteLine("Finish");
}
static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage);
}
Now Im using that code and calling that function Reader.ReadFile(); , file downloads good but nothing is written in console (e.percentage) .
Thanks
You are calling DownloadFile() before setting up your event handlers. The call to DownloadFile() will block your thread until the file has finished downloading, which means those event handlers will not be attached before your file has already downloaded.
You could switch the order around like so:
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFile("https://github.com/trapped/rotmg_svr/archive/master.zip", #"C:/Users/Asus/Desktop/aa.zip");
or you could use DownloadFileAsync() instead, which will not block your calling thread.
I have another question :(.
I'm trying to download multiple files for my application.
My question is: What do I have to do to check if the first download is done and then continue to the second download and so on?
This is my code atm:
private void DownloadBukkit()
{
MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by...";
webClient.DownloadFileAsync(new Uri(MySingleton.Instance.BukkitDownloadLink), Jar_Location);
webClient.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
webClient.DownloadFileCompleted += (webClient_DownloadFileCompleted);
}
private void DownloadDll()
{
if (!webClient.IsBusy)
{
MySingleton.Instance.FirstStartProgress = "Downloading HtmlAgilityPack.dll... Please stand by...";
webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation);
webClient2.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
webClient2.DownloadFileCompleted += (webClient2_DownloadFileCompleted);
}
}
void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
DownloadDll();
}
void webClient2_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Invoke((MethodInvoker)
Close);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Invoke((MethodInvoker)
delegate
{
labelProgress.Text = MySingleton.Instance.FirstStartProgress;
progressBar1.Value = e.ProgressPercentage;
});
}
I have checked this link: DownloadFileAsync multiple files using webclient but I didn't really understand how to implant this :(. (I'm quite new to C#)
The DownloadFileCompleted event is your signal to know that you are finished downloading the file.
From MSDN:
This event is raised each time an asynchronous file download operation
completes.
It's not clear from your code, where and how webClient and webClient2 are declared but essentially, you can start your second download when the first DownloadFileCompleted event is fired. Note, however, that you can perform the download of 2 different files concurrently provided you use 2 separate instances of WebClient.
Here is the fast forward code you can change it as per your requirement .
WebClient client = null;
public FileDownloader()
{
InitializeComponent();
client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;
}
void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
lblMessage.Text = "File Download Compeleted.";
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
lblMessage.Text = e.ProgressPercentage + " % Downloaded.";
}
private void StartDownload(object sender, RoutedEventArgs e)
{
if (client == null)
client = new WebClient();
//Loop thru your files for eg. file1.dll, file2.dll .......etc.
for (int index = 0; index < 10; index++)
{
//loop on files
client.DownloadFileAsync(
new Uri(
#"http://mywebsite.com/Files/File" + index.ToString() + ".dll"
, UriKind.RelativeOrAbsolute),
#"C:\Temp\file+" + index.ToString() + ".dll");
}
}