How to download file to specific location using WebClient's download - c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace Garfield
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "e.gif");
}
public void download(string link, string name)
{
using (WebClient Client = new WebClient())
{
Client.DownloadFile(link, name);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
Using the code above I downloaded an image online, now how do I set the location of that file to somewhere else instead of it being in the debug folder?

This can be done through mentioning a valid desired location and name when download.
Client.DownloadFile("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1",
#"C:\yourfolder\yourfilename.png");

You can pass an entire path to your download method:
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "C:\\Users\\(You)\\Documents\\e.gif");
Just replace everything before "e.gif" with the path you desire.
See more here: https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.8

Related

How to use created Instance on all forms / How to make my instance public C# windowsforms

Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1

C# Running program at startup

Hello I am working on c# simple project.
This is the code:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.IO;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath);
else
rk.DeleteValue(AppName,false);
}
namespace web1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// open browser
Thread.Sleep(6000);
System.Diagnostics.Process.Start("https://google.com/");
}
}
}
It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"
I want to:
If I click on this program .exe, it should run on the startup (every startup).
How to deal with it? I've tried so many ideas, but doesn't helped.
Thanks in advance.
BTW: it's my first time here, nice to meet you all!

An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe [duplicate]

This question already has answers here:
Why does Property Set throw StackOverflow exception?
(3 answers)
Closed 2 years ago.
I'm using C# to develop a windows forms application and I required to store certain values (Ex: UserID and Role), in order to use them again in various forms throughout the application.
The User ID and the Role will be changed with each login.
So tried using static classes.
To test it out first, I did the following.
Created "Form1" with a textbox and a button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
common.text = textBox1.Text;
Form2 obj = new Form2();
obj.Show();
}
}
}
Then created "Form 2" with only a label.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = common.text;
}
}
}
And to interconnect these two forms, created the following class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication3
{
public static class common
{
public static string text
{
get
{
return text;
}
set
{
text = value;
}
}
}
}
The purpose was to see if the label text on form2 would change when clicked on the button after entering text into the textbox in form1.
When running the code, the following error was thrown. Displays that this is thrown from the "set" method of the class.
An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe
If anyone could provide any clarity on this, it would be highly appreciated.
Thanks in advance.
Your set method is calling itself.
You need to add private string and change it and then return the changes via get.
Try this:
private static string _text;
public static string text
{
get
{
return _text;
}
set
{
_text = value;
}
}

How to send a list from a form to another one

I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}

Loading XmlDocument into Webform control

I have created a WCF service which sends an sms by using the supplied values. This then return XML in a string variable. I have managed to get the string loaded into an XmlDocument but then when I try and load it onto a Webform control I get an error because the XmlDocument is in memory and does not have a physical path. How can I get this loaded into my webform control(WB_XMLOut). Please see my code below what I have tried.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeadProcessTest.LeadProcessor;
using System.Xml;
namespace LeadProcessTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BT_Process_Click(object sender, EventArgs e)
{
LeadProcessorClient LP = new LeadProcessorClient();
string UName = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_UName.Text));
string PWord = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_Pass.Text));
string XMLIn = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_XMLin.Text));
LP.Open();
string Result = LP.Lead_Processing(UName, PWord, XMLIn);
LP.Close();
XmlDocument XML = new XmlDocument();
XML.LoadXml(Result);
WB_XMLOut.Url = new Uri(XML);
}
}
}

Categories