以下は、プロジェクトのログインフォームのボタンログインのコードです。フォームの一部として、データベースのログイン詳細を含むtxtファイルが作成されます。 Visual Studioから実行するだけなら、プロジェクトは正常に動作しますが、VSでexeを作成した後、connection.txtにアクセスできないというエラーが表示されます。 ありがとうございます。EXEがVSで作成された後にプログラムがエラーを発生する
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");
}
connection.txtがあるにすべきですか? –
project/bin/Debug/connection.txt – dshock9
現在のディレクトリ(パスは指定されていません)からファイルを読み込もうとしていますので、リリースアプリケーションが起動するときに、必要なファイルが最終的なアプリケーションがあるディレクトリと同じディレクトリになければなりません。ファイルがアプリケーションを起動したフォルダに存在するかどうか確認してください。 – Steve