我想创建一个更新功能。当出现新版本时,将出现一个输入框,其中包含一个下载按钮。当我按下该按钮时,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。