删除特定文件
Delete specific file
你能推荐一些代码吗,当我搜索邮政编码删除它时如何删除特定文件,如果我点击按钮将删除已经保存的文件。
private void button1_Click(object sender, EventArgs e)
{
try
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(EmployeeData);
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
只需使用以下代码即可:
private void button1_Click(object sender, EventArgs e)
{
try
{
string _path = "Employee.txt";
string EmployeeData = File.ReadAllText(_path);
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath(_path));
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
尝试下面的内容.. here 是供您参考的文档。
private void button1_Click(object sender, EventArgs e)
{
try
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath("Employee.txt"));
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
为了安全起见,您应该先检查文件是否存在..使用File.Exists
方法确保文件存在,然后才对文件执行read/delete操作。你应该避免 known exceptions
。使用下面的代码..
private void button1_Click(object sender, EventArgs e)
{
try
{
if (System.IO.File.Exists(Path.GetFullPath(@"Employee.txt"))
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath("Employee.txt"));
}
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
你能推荐一些代码吗,当我搜索邮政编码删除它时如何删除特定文件,如果我点击按钮将删除已经保存的文件。
private void button1_Click(object sender, EventArgs e)
{
try
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(EmployeeData);
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
只需使用以下代码即可:
private void button1_Click(object sender, EventArgs e)
{
try
{
string _path = "Employee.txt";
string EmployeeData = File.ReadAllText(_path);
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath(_path));
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
尝试下面的内容.. here 是供您参考的文档。
private void button1_Click(object sender, EventArgs e)
{
try
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath("Employee.txt"));
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}
为了安全起见,您应该先检查文件是否存在..使用File.Exists
方法确保文件存在,然后才对文件执行read/delete操作。你应该避免 known exceptions
。使用下面的代码..
private void button1_Click(object sender, EventArgs e)
{
try
{
if (System.IO.File.Exists(Path.GetFullPath(@"Employee.txt"))
{
string EmployeeData = File.ReadAllText("Employee.txt");
if (EmployeeData.Contains(textDelete.Text))
{
System.IO.File.Delete(Path.GetFullPath("Employee.txt"));
}
}
}
catch
{
MessageBox.Show("File or path not found or invalid.");
}
}