赛派号

什么手机1000左右性能好用又便宜 C#

boolFileFileInfoifreturn booltrue, falseFile.Exists

This C# method determines if a specific file exists. There are several ways of testing file existence. File.Exists is the easiest.

Exists() is the simplest way of checking that the file exists. This can prevent an exception from being thrown. File.Exists returns true or false.

Example

The usage of File.Exists is straightforward. To understand the example, you he to imagine that the files tested actually exist or don't exist on the hard disk.

Return The File.Exists method returns a Boolean value. We can test it in an if-conditional statement or assign its result to a bool.using System; using System.IO; class Program { static void Main() { // See if this file exists in the same directory. if (File.Exists("TextFile1.txt")) { Console.WriteLine("The file exists."); } // See if this file exists in the C:\ directory. [Note the @] if (File.Exists(@"C:\tidy.exe")) { Console.WriteLine("The file exists."); } // See if this file exists in the C:\ directory [Note the '\\' part] bool exists = File.Exists("C:\\lost.txt"); Console.WriteLine(exists); } }The file exists. The file exists. FalseInternals

The file existence method is well-known in many languages. This code shows how the File.Exists method is implemented in .NET—it calls into the InternalExists method.

path = Path.GetFullPathInternal(path); new FileIOPermission(FileIOPermissionAccess.Read, new string[] { path }, false, false).Demand(); flag = InternalExists(path);Comparing with FileInfo

When you create a new FileInfo with the constructor, it also uses the Path.GetFullPathInternal method and creates a new FileIOPermission object.

We used the File.Exists method to test for file existence in a C# program. This is a simple but powerful method that helps improve programs by shielding them from disk IO exceptions.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了