using System;
using System.Net;
using System.Windows.Forms;
namespace WindowsFormsApp1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e) {
login f2 = new login();
while (true) {
f2.ShowDialog();
if (f2.DialogResult == DialogResult.OK) {
Download();
} else if (f2.DialogResult == DialogResult.Cancel)
MessageBox.Show("Verification Failed");
break;
}
f2.Close();
}
//Download function
public void Download() {
SaveFileDialog sfd = new SaveFileDialog();
//Set the title of the save file dialog
sfd.Title = "Please select the file path to save.";
//Initialize the save directory, the default exe file directory
sfd.InitialDirectory = Application.StartupPath;
//Set the type of saved file
sfd.Filter = "Text file|*.txt|Audio file|*.wav|Picture file|*.jpg|All files|*.*";
if (sfd.ShowDialog() == DialogResult.OK) {
//Get the path to save the file
string filePath = sfd.FileName;
//save
try {
WebClient client = new WebClient();
client.DownloadFile(textBox1.Text, filePath);
} catch (WebException webEx) {
Console.Write(webEx.ToString());
}
}
}
}
}
登录窗口代码:
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp1 {
public partial class login : Form {
public login() {
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e) {
//Personal test database
string myconn = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = Test; Integrated Security = True";
SqlConnection conn = new SqlConnection(myconn);
string sql= $"select * from Test.dbo.demoAccount where userid='{ AccountTb.Text}' and password='{PassTb.Text}'";
conn.Open();
SqlCommand sqlCommand = new SqlCommand(sql, conn);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)//Satisfy the user name and password are consistent, enter the next interface
{
this.DialogResult = DialogResult.OK;
} else {
this.DialogResult = DialogResult.Cancel;
}
conn.Close();
}
}
}
2条答案
按热度按时间unftdfkk1#
我给予大家举个例子:
其核心思想是:单击下载后验证帐户密码,如果正确则下载。
使用DialogResult返回验证结果。
使用SaveFileDialog选择要保存的位置。
在文本框中输入要下载的URL。
主窗口代码:
登录窗口代码:
正确操作示意图:
Test Url
指令集
jaql4c8m2#
使用WebClient工具怎么样?您可以设置凭据并将响应写入文件。