System.IO.FileSystemInfo.Exists'不能像方法一样使用
我试图制作一个程序,该程序将删除文件购买我在运行代码时遇到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
}
}
FileInfo.Exists
是属性不是方法作为错误指示。它指示您是否与FileInfo
结构关联的文件(通过在创建时传递该路径)实际存在。
因此,所有你需要做的是检查:
if (myFileInfo.Exists)
{
}
如果你想检查不同路径,那么你需要使用File.Exists
,这是方法:
if (File.Exists(myPath))
{
}
或者如果您想删除与FileInfo
结构相关的文件:
myFileInfo.Delete();
顺便说一句,在下一行中,您应该使用File.Delete
而不是file.Delete
(这可能不会编译)。
请确定您了解File
和FileInfo
类别之间的区别,因为它似乎给您造成很大的麻烦。
另外下面的删除是完全错误的...... – Steve 2015-02-11 18:01:14
@Steve True,它需要使用'File'类的'static'方法。 – BradleyDotNET 2015-02-11 18:02:11
或只是'HealthIcon.Delete()'对于OP,我建议花一点时间阅读并理解FileInfo的文档,否则这个重复问题链将永远不会结束。 – Steve 2015-02-11 18:04:49
[非可调用成员'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
另一个问题已经搁置,没有答案。它应该可能只是被删除。 – 2015-02-11 18:03:37