System.IO.FileSystemInfo.Exists'不能像方法一样使用

时间:2015-02-11 17:56:14

标签: c#

我尝试制作一个删除文件的程序,然后在运行代码时遇到System.IO.FileSystemInfo.Exists cannot be used like a method异常。

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 个答案:

答案 0 :(得分:3)

FileInfo.Exists属性,而不是方法,如错误所示。它指示与FileInfo结构关联的文件(通过在创建它时传递该路径)是否实际存在。

所以你需要做的就是检查它:

if (myFileInfo.Exists)
{
}

如果您想检查不同的路径,则需要使用File.Exists,其中方法:

if (File.Exists(myPath))
{
}

或者,如果您要删除与FileInfo结构相关联的文件:

myFileInfo.Delete();

顺便说一句,在下一行中,您应该使用File.Delete而不是file.Delete(可能不会编译)。

请确保您了解FileFileInfo类之间的区别,因为这似乎会给您带来很多麻烦。