我的申请表中有login
表单和agent
表单。我的应用程序的第一个场景是当用户成功登录时,所有凭据(server , username, password, status
)将保存在文本文件中,我已经完成了。
我的第二种情况是,当用户关闭agent
表单并再次打开应用程序时,应用程序将读取文本文件中的所有凭据并将自动登录,我已经完成了但我的问题是当我打开应用程序时这两个表单都会显示login
和agent
表单,其中预期结果仅显示agent
表单。
以下是我的登录表单代码:
private void login_Load_1(object sender, EventArgs e)
{
Class.loginFunction logs = new Class.loginFunction();
string isLogged, user, pass, server;
try
{
isLogged = logs.readConfigFile(4);
user = logs.readConfigFile(2);
pass = logs.readConfigFile(3);
server = logs.readConfigFile(1);
if (isLogged.Equals("1"))
{
logs.loginAuthentication(user, pass);
}
else
{
txtServer.Text = server;
}
}
catch (Exception ex)
{
MessageBox.Show("Exception Thrown: " + ex.Message);
}
}
以下是登录方法的代码:
public void loginAuthentication(string agentId, string pass)
{
login idx = new login();
WebServiceInfo webInfo = new WebServiceInfo();
LoginInfo logInfo = new LoginInfo();
eDataNewUi eNewUi = new eDataNewUi();
try
{
logInfo.serverAddress = readConfigFile(1);
webInfo.url = "http://" + logInfo.serverAddress + "/eDataTran/service/main/agentLogin"; // store Url Of service in string
webInfo.jsonData = "{\"agentId\":\"" + agentId + "\" ,\"password\":\"" + pass + "\"}";
// Convert our JSON in into bytes using ascii encoding
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(webInfo.jsonData);
// HttpWebRequest
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(webInfo.url);
webrequest.Method = "POST";
webrequest.ContentType = "application/json";
webrequest.ContentLength = data.Length;
// Get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// Declare & read the response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// Fetch the response from the POST web service
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string result = loResponseStream.ReadToEnd();
loResponseStream.Close();
JObject o = JObject.Parse(result);
string responseCode = o["Data"][0]["ResponseCode"].ToString();
switch (responseCode)
{
case "0":
MessageBox.Show("Username and Password is not valid");
break;
case "1":
idx.Hide(); // this part where the form is not hiding
eNewUi.Show();
eNewUi.tssUserId.Text = "User Id: " + agentId;
getAgentId(agentId);
break;
}
webresponse.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception Response: " + ex.Message);
}
}
这是我的program.cs
static Mutex mutex = new Mutex(false, "oreilly.com OneAtATimeDemo");
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
{
MessageBox.Show("Application is running already!");
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new login());
}
finally
{
mutex.ReleaseMutex();
}
}
答案 0 :(得分:2)
根据方案的信息,您需要检查是否生成了Textfile。如果生成了这个TextFile,您应该在Program.cs
上添加一个检查方法,否则您的应用程序将触发LoginFrm
示例代码:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(TextFileChecker())
{
Application.Run(new AgentFrm());
}
else
{
Application.Run(new LoginFrm());
}
}
private bool TextFileChecker()
{
//run a checking method for the textfile
}
}
答案 1 :(得分:1)
你没有提到关于Program.cs
的任何内容,但我看到了一些类似的问题,所以我假设内容与以下内容类似
private static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
应用程序已经为您和方法Login
loginAuthentication
对象
public void loginAuthentication(string agentId, string pass){
login idx = new login();//Here you created another Login object
....
}
就我而言,你的程序成功地归结为“1”:
switch (responseCode) {
case "0":
MessageBox.Show("Username and Password is not valid");
break;
case "1":
//This is the part where you are trying to hide a form, which is not even shown
idx.Hide();
建议主要:
private static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var hasCredentialFile = CheckCredentialFile();
Application.Run(hasCredentialFile ? new Agent():Login());
}