以下是我项目中登录表单上的按钮登录代码。作为表单的一部分,将创建一个包含数据库登录详细信息的txt文件。如果我只是从visual studio运行,项目运行正常,但是在我用VS创建一个exe后,我得到错误,说无法访问connection.txt。 谢谢。
private void btnlogin_Click(object sender, EventArgs e)
{
try
{
// if the file doesn't exist.
string fromConnFile = File.ReadAllText("connection.txt");
NetworkConn ncBefore = JsonConvert.DeserializeObject<NetworkConn>(fromConnFile);
String serverName = Base64.base64decode(ncBefore.servername);
String userName = Base64.base64decode(ncBefore.username);
String passWord = Base64.base64decode(ncBefore.password);
String dataBase = Base64.base64decode(ncBefore.database);
NetworkConn ncAfter = new NetworkConn()
{
servername = serverName,
username = userName,
password = passWord,
database = dataBase
};
String user = txtusername.Text.TrimStart().TrimEnd();
String pass = txtpassword.Text.TrimStart().TrimEnd();
// object for connecting database.
MysqlQuery dataQuery = new MysqlQuery(serverName, userName, passWord, dataBase);
DataTable userInfo = new DataTable();
int ifSucceed = dataQuery.Login(user.Replace("'", ""), pass.Replace("'", ""), userInfo);
if (ifSucceed == 1)
{
if (userInfo.Rows.Count == 1)
{
if (userInfo.Rows[0][0].ToString() == "admin")
{
this.Hide();
Admin newform = new Admin(dataQuery, userInfo);
newform.Closed += (s, args) => this.Close();
newform.Show();
}
}
else if (userInfo.Rows.Count > 1)
{
Console.WriteLine("There are more than 1 rows in the table");
}
else
{
Console.WriteLine("Please check");
}
}
}
catch (FileNotFoundException fnfe)
{
MessageBox.Show("Please set the network");
}
答案 0 :(得分:0)
应用程序exe
和connection.txt
文件应位于同一文件夹