2015-02-11 133 views
-2

我試圖製作一個程序,該程序將刪除文件購買我在運行代碼時遇到System.IO.FileSystemInfo.Exists cannot be used like a method異常。System.IO.FileSystemInfo.Exists'不能像方法一樣使用

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
namespace WindowsFormsApplication5 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     FileInfo HealthIcon = new FileInfo(@"Holo\Normal\hud_health.texture");      
     FileInfo file = new FileInfo(@"hud_health.texture"); 

     ///hud icons textures(Normal)          
     private void button1_Click(object sender, EventArgs e) ///Health icon normal 
     { 
      ///Health icon textures(Normal) 
      if (HealthIcon.Exists(@"C:\Program Files (x86)\Steam\steamapps\common\PAYDAY 2\assets\mod_overrides\HoloHud\guis\textures\pd2\hud_health.texture")) 
      { 
       file.Delete(@"C:\Program Files (x86)\Steam\steamapps\common\PAYDAY 2\assets\mod_overrides\HoloHud\guis\textures\pd2\hud_health.texture"); 
      } 
      HealthIcon.CopyTo(@"C:\Program Files (x86)\Steam\steamapps\common\PAYDAY 2\assets\mod_overrides\HoloHud\guis\textures\pd2"); ///Health icon Normal 
     } 
} 
+1

[非可調用成員'System.IO.FileSystemInfo.Exists'的可能重複不能像方法一樣使用](http://stackoverflow.com/questions/28459343/non-invocable-member-system-io- filesysteminfo-exists-can-be-used-like-a-met) – 2015-02-11 17:59:48

+1

另一個問題已經擱置,沒有答案。它應該可能只是被刪除。 – 2015-02-11 18:03:37

回答

3

FileInfo.Exists屬性不是方法作爲錯誤指示。它指示您是否與FileInfo結構關聯的文件(通過在創建時傳遞該路徑)實際存在。

因此,所有你需要做的是檢查:

if (myFileInfo.Exists) 
{ 
} 

如果你想檢查不同路徑,那麼你需要使用File.Exists,這方法:

if (File.Exists(myPath)) 
{ 
} 

或者如果您想刪除與FileInfo結構相關的文件:

myFileInfo.Delete(); 

順便說一句,在下一行中,您應該使用File.Delete而不是file.Delete(這可能不會編譯)。

請確定您瞭解FileFileInfo類別之間的區別,因爲它似乎給您造成很大的麻煩。

+0

另外下面的刪除是完全錯誤的...... – Steve 2015-02-11 18:01:14

+0

@Steve True,它需要使用'File'類的'static'方法。 – BradleyDotNET 2015-02-11 18:02:11

+2

或只是'HealthIcon.Delete()'對於OP,我建議花一點時間閱讀並理解FileInfo的文檔,否則這個重複問題鏈將永遠不會結束。 – Steve 2015-02-11 18:04:49

相關問題