2016-11-03 33 views
1

我試圖刪除我的文件內的內容。但是當我從菜單中選擇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"); 
     } 
    } 
} 
+1

調試器。調試器。調試器。 –

+0

它沒有顯示任何東西,但thnx爲了讓你的時間和回答我的問題:) –

+0

如果調試器沒有顯示任何東西,那麼你忘了開始你的程序。使用調試器啓動程序,使用斷點,並逐步執行代碼。 –

回答

0

你可以把一條線

File.Create("movie.txt"); 

下面

File.Delete("movie.txt"); 

因爲你喜歡它可能無法正常工作,但它做相同的工作,不是嗎?

+0

但這會創建一個文件沒有? –

+0

我想從菜單中選擇3時刪除文件中的所有內容 –

+0

只需添加該行並自行嘗試即可。 :-) – uqji

2

事實上的System.IOFile.Delete方法將刪除該目錄中的文件,如果它存在於目錄中,要保持文件和必須刪除其內容意味着你必須使用File.WriteAllText()方法:

這將創建一個新文件,將內容寫入文件,然後關閉文件。如果目標文件已經存在,則覆蓋 。

string path="path to your file"; // Let it be "movie.txt" 
File.WriteAllText(path, ""); 

答案從註釋的第二個問題:

如果你需要的東西(內容)添加到現有的文件,那麼你必須使用File.AppendAllText()方法,

哪樣打開一個文件,將指定字符串的文件, 然後關閉該文件。如果該文件不存在,則此方法創建一個文件 ,將指定的字符串寫入該文件,然後關閉該文件。

在這種情況下,段應該是這樣的:

File.AppendAllText(path, "Some content to append"); 
+0

workssss tnx man你最好 –

+0

酷....很高興幫助你 –

+0

多一個問題 –