我試圖刪除我的文件內的內容。但是當我從菜單中選擇3時,它會從文件夾中刪除我的文件,而不是從文件中刪除。你可以幫我嗎?我從昨天開始一直在努力。如何刪除c#中文件的內容?
謝謝。
{
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");
}
}
}
調試器。調試器。調試器。 –
它沒有顯示任何東西,但thnx爲了讓你的時間和回答我的問題:) –
如果調試器沒有顯示任何東西,那麼你忘了開始你的程序。使用調試器啓動程序,使用斷點,並逐步執行代碼。 –