需要帮助检查文件是否存在?

时间:2020-05-29 06:40:49

标签: c# .net

我有一种方法可以使用一个按钮来保存文件,该按钮可以抓取文本框中的文本,并使用其中一个文本框中指定的数字保存文件。例如:“输入雇员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)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

更改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";