1

我想创建一个更新功能。当出现新版本时,将出现一个输入框,其中包含一个下载按钮。当我按下该按钮时,saveFileDialog 将出现以将文件(新版本)保存在我想要的位置。

我有这个功能:

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");
            }

        }
    }

首先我尝试使用一个测试按钮来查看该功能是否有效,然后我将在输入框中实现。

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

当我按下按钮时,会出现 saveFileDiaglog 并且在崩溃之后。任何 saveFileDialog 都会出现崩溃。如果我只有一个没有任何代码的 saveFileDialog,会崩溃,问题是 saveFileDialog。

4

5 回答 5

1

删除第一行“saveFileDialog1.ShowDialog();” 没用!!

消除

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

从你的函数 descarcare();

你应该像这样创建它。

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

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

为了获得更好的代码,您应该将 SaveFileDialog1 设置为类似这样。

....
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
....

在 catch 块中,您还应该将“消息”放到输出中。

于 2012-08-27T18:46:16.180 回答
1

像这样修改你的代码:

   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");
       }

   }
于 2012-08-27T17:53:30.410 回答
1

这是你的问题

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

你需要改变你正在做的代码 DialogResult result = saveFileDialog1.ShowDialog(); 至

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

var result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
  //do your logic here.. 
}
于 2012-08-27T17:48:00.640 回答
0

使用 silverlight,由于安全限制,它们不会为您提供正在保存的文件的位置。但是,您可以使用OpenFile函数为用户选择的文件获取一个 Stream 对象。

查看这篇文章,了解如何将 WebClient 与 Stream 对象一起使用。

于 2012-08-27T17:35:25.913 回答
0

建议将代码重组为:-

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");
       }

   }

进一步的 saveFileDialog1.FileName 给出了必须保存文件的完整路径,因此它不应该成为问题。

但是我会建议使用 webClient.DownloadFileAsync而不是webClient.DownloadFile因为稍后是阻塞调用。

于 2012-08-27T17:51:45.943 回答