2016-05-29 12 views
0

以下は、プロジェクトのログインフォームのボタンログインのコードです。フォームの一部として、データベースのログイン詳細を含む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"); 
    } 
+0

connection.txtがあるにすべきですか? –

+0

project/bin/Debug/connection.txt – dshock9

+1

現在のディレクトリ(パスは指定されていません)からファイルを読み込もうとしていますので、リリースアプリケーションが起動するときに、必要なファイルが最終的なアプリケーションがあるディレクトリと同じディレクトリになければなりません。ファイルがアプリケーションを起動したフォルダに存在するかどうか確認してください。 – Steve

答えて

0

アプリケーションexeconnection.txtファイルが同じフォルダ

+0

どのようにしてconnection.txtが同じフォルダにあるかを確認します。 VSでプロジェクトを実行している間、connection.txtはプロジェクトのデバッグフォルダに作成されます。私はexeを作成している間に何をしなければならないのですか?私が得るエラーは "c:\ ProgramFiles(x86)\ project \ connection.txtへのパスは拒否されました。" – dshock9

+0

あなたは管理者としてアプリケーションを実行しようとしましたか? –

関連する問題