Async download using checkboxes - c#

I have created a WPF C# GUI that should work as a multiple downloader for tools. checkboxes for each app and a download button.
Could anybody help me with a startup for the download code? Very new to C# and while I googled a lot I still haven't found something that works for my case.
The download button should check which of the checkboxes are checked and download the files asynchronously.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ccleaner.IsChecked == true)
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string url = "https://www.ccleaner.com/ccleaner/download/portable/downloadfile";
string filename = "ccleaner.zip";
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new Uri(url), desktopPath + "/" + filename);
}
}
The error I'm getting is:
Method name expected on line 10 of mainwindow.xaml
which looks like this:
<CheckBox x:Name="ccleaner" Content="ccleaner" HorizontalAlignment="Left" Margin="197.883,90.123,0,0" VerticalAlignment="Top" Checked="ccleaner"/>

As auburg note I had to remove "Checked="ccleaner"" from my xaml code.
Sorry that I can't mark his comment as the solution

Related

C# project, problem how use files in project directory Resources

i did research about this, but nothing worked.
I created folder in my project in Visual Studio 2019 => Images and i placed a file footer.png here.
In Visual Studio => Right click on folder Images, added new item Resources and add this line => Name -> FooterBG and Value -> Images/footer.png
in code:
string pathToFooter = Resource.FooterBG;
in debug get return Images\footer.png
When i want to add this to string with html tags, dont work:
String htmlTags = "<table background='"+ pathToFooter + "'><tbody><tr><td></td></tr></tbody></table>";
Please any tips what im doing wrong ?
Based on my test, the pathToFooter is not the actual path of the image.
Here is a code example you can refer to:
private void button1_Click(object sender, EventArgs e)
{
string pathToFooter = Resource1.FooterBG;
string path = AppDomain.CurrentDomain.BaseDirectory + Path.Combine(#"../..", pathToFooter);
String htmlTags = "<table background='" + path + "' width='1000' height='800' align='center'><tbody><tr><td></td></tr></tbody></table>";
webBrowser1.DocumentText = htmlTags;
}
Result:

How to fix C# google search

So I'm creating a program that can go into google and search for the word that has
been saved into a variable. So my question is how I can do this, without having it go to a URL instead just going to Google and search "whatever" Here's the code.
private void button2_Click(object sender, EventArgs e)
{
if (Script.Text == Script.Text)
{
Console.AppendText("\n[1] Loading Websites of " + Script.Text + "...");
}
WebClient wc = new WebClient();
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "Scripts " + Script.Text);
}
This should solve your problem
public static void GoToSite(string url)
{
System.Diagnostics.Process.Start("chrome.exe", url);
}
you can also change browser dynamically by adding a second params
You can also do this:
String stringToSearch = “stackoverflow”;
System.Diagnostics.Process.Start(#"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com.au/search?q=" + stringToSearch);
This assumes IE is the default browser on your machine. If Chrome or Firefox is the default browser, then code will fail.
This is a useful link:
http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp
Maybe you can put it in a try catch block and see if there is an exception. If there is an exception with starting IE, then open chrome.

Getting a what i believe to be a webclient error in C#, Need help Please

Okay so i am fairly new to programming and was told to just start acouple projects and google/ask if i need hep so here i am, im making a program that will download and run a file from the internet and when i run the release app i get this error http://gyazo.com/7b017f0ed550af3b86b24ad480db8fe8 so i press the details and it give me this long error report thing http://pastebin.com/Ag7u9gBZ i dont seem to have enough knowledge to be able to read and debug it, and i am very confussed, from what i see it has to do with system.io and webclient and path or something i dont know.
private void startBotToolStripMenuItem_Click(object sender, EventArgs e)
{
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21FB6AD2A0A1EB413347302A46C5FB1A39599DF740D6&directstart=1";
WebClient wc = new WebClient();
wc.DownloadFile(direct_exe_from_url, filepath);
Process.Start(filepath);
}
You're getting a System.IO.DirectoryNotFoundException so I would guess that this: C:\Users\Owner\AppData\Roaming\Downloaded Files\ directory does not exist.
You can use the Directory.CreateDirectory method to create the directory before putting the downloaded file in there, e.g:
string directoryPath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\";
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21
//Rest of your code

How to load javascript in windows 8

I am looking for a way to implement Internet explorer - a desktop version in my Winodws 8 app . This is caused by my c# application- i need to run javascript page but its impossible in metro version of IE. Is there any method to do this?
I `ll describe my problem: when i want to load html of DOM page(rendered by javascript) i cant because javascript can run (I use WebView). I tried to open this page using Desktop version and it worked perfectly. Unfortunately Metro IE10 cant show the content and my WebView too.
Thanks for help.
You can sure inject in the c# webview a javascript file:
This is the action for a button:
private async void Lamp_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///js/injected.js");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
string text = await FileIO.ReadTextAsync(file); //reading content as string
var script = "(function(){" + text + "})()";
}
try
{
var result = wv.InvokeScript("eval", new string[] { script });
}
catch (Exception) { }
wv is you id name for the webview element in the xaml

Downloaded file using webclient.DownloadFileAsync has 0KB

I'm trying to download zend-framework (from http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip) simply using WebClient
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader= new WebClient();
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
The file is created, but it is empty. I checked response using fiddler and I get HTTP 200, correct content-length but "connection: closed" and fiddler shows "-1" in "body" column.
I have tried adding user agent (copied from google chrome request) and "connection: keep-alive" to headers, but none of these helped. I'm also pretty sure, that my program downloaded this file using the same URL once or twice before. There are no errors in events fired by WebClient.
Any ideas?
Ok, I finnaly found the answer! Before downloading the file, I was checking its size by sending HttpWebRequest. The problem was, that i didn't Close() the response.
Thanks for the answers, they were nice clues.
Just my guess: maybe you can try to keep the WebClient instance in some place would not be garbage collected. When the DownloadFileCompleted event fired, you just clean the reference to the WebClient instance and let GC to reclaim the memory later (and don't forget to call Dispose method).
Try to handle the DownloadProgressChanged and DownloadFileCompleted event.
private void button1_Click(object sender, EventArgs e)
{
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
}
void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
MessageBox.Show(e.Error.Message);
else
MessageBox.Show("Completed!!!");
}
If you have UAC enabled in Windows "C:\temp.zip" in the following line will fail to save the file because you aren't allowed to write outside of user directories without elevated permissions:
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");

Categories