我有一种方法,使用一个按钮保存文件,该按钮在文本框中抓取文本,并以其中一个文本框中指定的数字保存它们。例如:“输入employee id:”(我将输入) " 12345“并单击按钮,然后将该文件保存为12345,其中包含其他文本框中的一些信息。
现在,我还实现了一种检查该文件是否存在于两个单独文件夹中的方法,这样就不会创建多个同名文件。
现在,当有人单击一个按钮在单独的页面上登录时,我再次尝试检查该文件是否存在(两个目录中),但是我遇到了一个问题,它总是告诉我文件不存在。我刚开始编程,所以有点迷茫。这是我的代码:
登录页:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rent_a_Car
{
public partial class Employee_Login_Page : Form
{
public Employee_Login_Page()
{
InitializeComponent();
}
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers\Manager_Ids.txt"; //Path To Manager Logins
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff\Staff_Ids.txt"; //Path to Employees Logins
string FileName; //Declares FileName
bool FileExists;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Employee_Id_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && //Checks Characters entered are Numbers Only and allows them
(e.KeyChar != '0'))
{
e.Handled = true;
}
else if (e.KeyChar == (char)13) //Checks if The "Enter" Key is pressed
{
Login_Btn_Click(this, new EventArgs()); //If so activate the Login Button
}
}
private void Employee_Login_Page_Load(object sender, EventArgs e)
{
}
private void Login_Btn_Click(object sender, EventArgs e)
{
FileName = Employee_Id_TextBox.Text=".txt"; //sets the string FileName to Employee Id Entered
string pathManagerString = System.IO.Path.Combine(ManagerPath, FileName); //Combine Manager Path With New Id and sets to pathManagerString
string pathEmployeeString = System.IO.Path.Combine(EmployeePath, FileName); //Combine Manager Path With New Id and sets to pathEmployeeString
if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in Employee folder
{
MessageBox.Show("Employee ID Entered as Employee");
EmployeeHome_Page myForm = new EmployeeHome_Page();
this.Hide();
myForm.ShowDialog();
}
else if(FileExists = File.Exists(pathManagerString))
{
MessageBox.Show("Employee ID Entered as Manager");
EmployeeHome_Page myForm = new EmployeeHome_Page();
this.Hide();
myForm.ShowDialog();
}
else
{
MessageBox.Show("Employee ID Not Found");
}
//need to check logins and wether they are manager or just standard users/and if so activate manager button
}
}
}第二形式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rent_a_Car
{
public partial class AddEmployee_Page : Form
{
public AddEmployee_Page()
{
InitializeComponent();
}
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers";
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff";
private void AddEmployee_Page_Load(object sender, EventArgs e)
{
}
string FileName; //Declares FileName
bool FileExists;
private void CancelAddEmployee_Btn_Click(object sender, EventArgs e)
{
EmployeeHome_Page myForm = new EmployeeHome_Page ();
this.Hide();
myForm.ShowDialog();
}
private void AddEmployee_Btn_Click(object sender, EventArgs e)
{
FileName = EmployeeID_AddEmployee_TextBox.Text; //sets the string FileName to Employee Id Entered
string pathManagerString = System.IO.Path.Combine(ManagerPath, FileName); //Combine Manager Path With New Id and sets to pathManagerString
string pathEmployeeString = System.IO.Path.Combine(EmployeePath, FileName); //Combine Manager Path With New Id and sets to pathEmployeeString
if (FirstName_AddEmployee_TextBox.Text == "" || LastName_AddEmployee_TextBox.Text == "" || EmployeeID_AddEmployee_TextBox.Text == "") //checks if any feild are empty if so show prompt
{
MessageBox.Show("Missing Information!");
}
else if (EmployeeID_AddEmployee_TextBox.TextLength < 5) //checks if employee id is less then 5 digits long, if so display message
{
MessageBox.Show("ID Needs To Be Five Numbers Long");
}
else if (IsManager_CheckBox.Checked) //checks if manager
{
if (FileExists = File.Exists(pathManagerString)) //Check If Employee ID File Exists in manager folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in employee folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(pathManagerString, true))
{
file.WriteLine("Manager First Name: " + FirstName_AddEmployee_TextBox.Text); //Writes First Name
file.WriteLine("Manager Last Name: " + LastName_AddEmployee_TextBox.Text);//Writes Last Name
}
}
}
else
{
if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in emoployee folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else if(FileExists = File.Exists(pathManagerString)) //Check If Employee ID File Exists in manager folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(pathEmployeeString, true))
{
file.WriteLine("Employee First Name: " + FirstName_AddEmployee_TextBox.Text); //Writes First Name
file.WriteLine("Employee Last Name: " + LastName_AddEmployee_TextBox.Text);//Writes Last Name
}
}
}
}
private void FirstName_AddEmployee_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void EmployeeID_AddEmployee_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && //Checks Characters entered are Numbers Only and allows them
(e.KeyChar != '0'))
{
e.Handled = true;
}
}
private void IsManager_CheckBox_CheckedChanged(object sender, EventArgs e)
{
}
}
}发布于 2020-05-29 06:51:18
更改Login_Btn_Click方法的这一行代码:
年长的
FileName = Employee_Id_TextBox.Text=".txt"; 替换
FileName = Employee_Id_TextBox.Text+".txt"; 并更新两个文件夹的位置:
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers";
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff";https://stackoverflow.com/questions/62080088
复制相似问题