如何在c#中删除文件的内容?
我正在尝试删除文件中的内容。但当我从菜单中选择3时,它会从文件夹中删除我的文件,而不是从文件中删除。你能帮我吗?我从昨天开始就在做这件事 多谢各位如何在c#中删除文件的内容?,c#,delete-file,C#,Delete File,我正在尝试删除文件中的内容。但当我从菜单中选择3时,它会从文件夹中删除我的文件,而不是从文件中删除。你能帮我吗?我从昨天开始就在做这件事 多谢各位 { class test { static void Main(string[] args) { movie[] arr = new movie[1]; bool exit = false; while (exit == fals
{
class test
{
static void Main(string[] args)
{
movie[] arr = new movie[1];
bool exit = false;
while (exit == false)
{
menu();
string choice = Console.ReadLine();
switch (choice)
{
case "1":
add(ref arr);
break;
case "2":
saveData(ref arr);
break;
case "3":
deleteData(ref arr);
break;
case "4":
exit = true;
break;
}
}
}
// adding data
public static void add(ref movie[] arr)
{
StreamWriter writer = new StreamWriter("movie.txt");
writer.WriteLine(arr.Length + 1);
// creat new object
movie temp = new movie();
// collect data fromuser
Console.WriteLine("enter title");
temp.Title = Console.ReadLine();
Console.WriteLine("enter director");
temp.Director = Console.ReadLine();
Console.WriteLine("enter year");
temp.Year = Convert.ToInt32(Console.ReadLine());
// write the new data to text file
writer.WriteLine(temp.Title);
writer.WriteLine(temp.Director);
writer.WriteLine(temp.Year);
writer.Close();
//
}
// save data
public static void saveData(ref movie[] arr)
{
StreamReader reader = new StreamReader("movie.txt");
int size = Convert.ToInt32(reader.ReadLine());
arr = new movie[size];
for (int index = 0; index < arr.Length; index++)
{
arr[index] = new movie();
arr[index].Title = reader.ReadLine();
arr[index].Director = reader.ReadLine();
arr[index].Year = Convert.ToInt32(reader.ReadLine());
}
reader.Close();
}
public static void deleteData(ref movie[] arr)
{
if(File.Exists("movie.txt"))
{
File.Delete("movie.txt");
}
else
{
;//donothing
}
}
// menu
private static void menu()
{
Console.WriteLine("plz chosse one of the following options to continue ");
Console.WriteLine("1: Add Movie");
Console.WriteLine("2: Save data");
Console.WriteLine("3: delete Movie");
Console.WriteLine("4: Exit");
}
}
}
{
课堂测试
{
静态void Main(字符串[]参数)
{
电影[]arr=新电影[1];
bool exit=false;
while(exit==false)
{
菜单();
字符串选项=Console.ReadLine();
开关(选择)
{
案例“1”:
添加(参考arr);
打破
案例“2”:
保存数据(参考arr);
打破
案例“3”:
删除数据(参考arr);
打破
案例“4”:
退出=真;
打破
}
}
}
//添加数据
公共静态无效添加(参考电影[]arr)
{
StreamWriter=newstreamwriter(“movie.txt”);
写入线(arr.Length+1);
//创建新对象
电影温度=新电影();
//从用户处收集数据
Console.WriteLine(“输入标题”);
temp.Title=Console.ReadLine();
Console.WriteLine(“输入控制器”);
temp.Director=Console.ReadLine();
控制台写入线(“输入年份”);
temp.Year=Convert.ToInt32(Console.ReadLine());
//将新数据写入文本文件
编剧行(临时职称);
编剧(临时董事);
编剧行(临时年份);
writer.Close();
//
}
//保存数据
公共静态void saveData(ref movie[]arr)
{
StreamReader=新的StreamReader(“movie.txt”);
int size=Convert.ToInt32(reader.ReadLine());
arr=新电影[尺寸];
对于(int index=0;index
你只需写一行
File.Create("movie.txt");
下面
File.Delete("movie.txt");
它可能不会像您喜欢的那样工作,但它做的是相同的工作,不是吗?实际上是
文件。删除系统的方法。IO
将从目录中删除该文件(如果该文件存在于目录中),要保留该文件并删除其内容,意味着您必须使用以下方法:
它将创建一个新文件,将内容写入该文件,然后
然后关闭文件。如果目标文件已存在,则为
覆盖
回答评论中的第二个问题:
如果需要向现有文件添加内容,则必须使用以下方法:
它将打开一个文件,将指定的字符串附加到该文件,然后
然后关闭文件。如果文件不存在,此方法将创建
一个文件,将指定的字符串写入该文件,然后关闭该文件
在这种情况下,代码段应如下所示:
File.AppendAllText(path, "Some content to append");
调试器。调试器。调试器。它没有向我显示任何内容。但是谢谢你花时间回答我的问题:)如果调试器没有显示任何内容,那么你忘记启动程序了。用调试器启动程序,使用断点,然后一步一步地执行代码。但这会创建一个文件否?我想在我从菜单中选择3时删除文件中的所有内容只需添加行,然后自己尝试即可。:-)它正在工作,兄弟,但当我打开文件时,一个windows弹出窗口说该进程无法访问文件bcz,它正被另一进程使用。workssss tnx man u r the bestCool。。。。很高兴再帮你一个问题问题不清楚,你必须更具体一点假设我输入了3部电影的名称,我从菜单中选择了2,它应该将3部电影的名称保存在文件中。但是当我输入3部电影的名称时,它只保存最后一部。我如何解决这个问题?
File.AppendAllText(path, "Some content to append");