C#检查文件的目标是有效的 [英] C# check that a file destination is valid

查看:101
本文介绍了C#检查文件的目标是有效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有检查指定的目录是有效的一个标准功能?

Is there a standard function to check that a specified directory is valid?

我想问的原因是,我收到来自用户的绝对目录字符串和文件名我想合法性检查的位置,以检查它是否有效。

The reason I ask is that I am receiving an absolute directory string and filename from a user and I want to sanity check the location to check that it is valid.

推荐答案

有关文件

File.Exists(string)

对于目录

Directory.Exists(string)

注:
如果要重复使用一个对象,你应该考虑使用FileInfo类VS静态文件类。 File类的静态方法确实可能的不必要的安全检查各一次。

FileInfo的 - 的DirectoryInfo - 的File - 的目录

NOTE: If you are reusing an object you should consider using the FileInfo class vs the static File class. The static methods of the File class does a possible unnecessary security check each time.
FileInfo - DirectoryInfo - File - Directory

 FileInfo fi = new FileInfo(fName);
 if (fi.Exists)
    //Do stuff

DirectoryInfo di = new DirectoryInfo(fName);
 if (di.Exists)
    //Do stuff

这篇关于C#检查文件的目标是有效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆