AnDr3yy
AnDr3yy

Reputation: 239

saveFileDialog stop working + download a file from internet

I want to creat a function for update. When a new version appears, a inputbox will appears with a button for download. When I push that button, saveFileDialog will appear to save the file (new version) where I want.

I have this function:

public void descarcare()
    {

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.CreatePrompt = true;
        saveFileDialog1.OverwritePrompt = true;
        saveFileDialog1.FileName = "myText";
        saveFileDialog1.DefaultExt = "txt";
        saveFileDialog1.Filter ="Text files (*.txt)|*.txt|All files (*.*)|*.*";
        saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        DialogResult result = saveFileDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {

            try
            {
                WebClient webClient = new WebClient();
                byte[] receivedData = webClient.DownloadData("http://startut.ro/smartAppointment.rar");
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);

                bw.Write(receivedData);

                bw.Close();
                fs.Close();
                ((IDisposable)fs).Dispose();
            }
            catch (Exception e)
            {
                string message = e.Message;
                MessageBox.Show("Nu ai o conexiune de internet stabilită. Încearcă să te conectezi la internet, și după aceea să descarci noua versiune !", "EROARE CONEXIUNE INTERNET");
            }

        }
    }

Firstly I try with a test button to see if the function work, and after that I will implement in the inputbox.

 private void button14_Click(object sender, EventArgs e)
        {
             descarcare();
        }

When i push the button, the saveFileDiaglog will appear and after that crash. The crash appear for any saveFileDialog. If i have only an saveFileDialog without any code, will crash, the probleme is the saveFileDialog.

Upvotes: 1

Views: 1017

Answers (5)

MethodMan
MethodMan

Reputation: 18863

Here is your problem

**DialogResult result = saveFileDialog1.ShowDialog();**

you need to change the code where you are doing DialogResult result = saveFileDialog1.ShowDialog(); to

SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
// you need to Create an Instance of SaveFileDialog

var result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
  //do your logic here.. 
}

Upvotes: 1

moskito-x
moskito-x

Reputation: 11968

Remove first line "saveFileDialog1.ShowDialog();" It is useless !!

remove

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

from your function descarcare();

You should create it like this.

public partial class Form1: Form
{
....
    internal SaveFileDialog SaveFileDialog1;
....

    public Form1() : base()
    {   
                ....
                this.SaveFileDialog1 = new SaveFileDialog();
                ....

for better code, you should set SaveFileDialog1 something like this.

....
SaveFileDialog1.CreatePrompt = true;
SaveFileDialog1.OverwritePrompt = true;
SaveFileDialog1.FileName = "myText";
SaveFileDialog1.DefaultExt = "txt";
SaveFileDialog1.Filter = 
            "Text files (*.txt)|*.txt|All files (*.*)|*.*";
SaveFileDialog1.InitialDirectory = 
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = SaveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
            {

                try
....

In the catch block you also should put the "message" to the output.

Upvotes: 1

Thanatos
Thanatos

Reputation: 1186

Modify your code like this:

   saveFileDialog1.ShowDialog();

   if (saveFileDialog1.ShowDialog() == DialogResult.OK)
   {

       try
       {
           WebClient webClient = new WebClient();
           byte[] receivedData = webClient.DownloadData("http://startut.ro/smartAppointment.rar");
           FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
           BinaryWriter bw = new BinaryWriter(fs);

           bw.Write(receivedData);

           bw.Close();
           fs.Close();

       }
       catch (Exception e)
       {
           string message = e.Message;
           MessageBox.Show("Nu ai o conexiune de internet stabilită. Încearcă să te conectezi la internet, și după aceea să descarci noua versiune !", "EROARE CONEXIUNE INTERNET");
       }

   }

Upvotes: 1

perilbrain
perilbrain

Reputation: 8207

suggested to restructure the code as:-

WebClient webClient = new WebClient();    
saveFileDialog1.ShowDialog();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler (FileDownloaded);//Implement this method to notify the download.

   if (saveFileDialog1.ShowDialog() == DialogResult.OK)
   {

       try
       {               
           webClient.DownloadFileAsync("http://startut.ro/smartAppointment.rar", saveFileDialog1.FileName);
       }
       catch (Exception e)
       {
           string message = e.Message;
           MessageBox.Show("Nu ai o conecsiune de internet stabilită. Încearcă să te conectezi la internet, și după aceea să descarci noua versiune !", "EROARE CONECSIUNE INTERNET");
       }

   }

Further saveFileDialog1.FileName gives the fullpath where the file has to be saved so it should not be an issue.

However I will suggest using webClient.DownloadFileAsync instead of webClient.DownloadFile because later is a blocking call.

Upvotes: 0

DanTheMan
DanTheMan

Reputation: 3277

With silverlight, due to security restrictions, they will not give you the location of the file you are saving. You may, however, get a Stream object to the file the user selected by using the OpenFile function.

Take a look at this article for how to use the WebClient still with the Stream object.

Upvotes: 0

Related Questions