У меня есть способ сохранять файлы с помощью кнопки, которая захватывает текст в текстовых полях и сохраняет их с номером, указанным в одном из этих текстовых полей. пример: «введите идентификатор сотрудника:» (я бы ввел) «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)
{
}
}
}