简体   繁体   English

我应该使用什么类型来表示参数是C#中的文件名?

[英]What type should I use to indicate an argument is a filename in C#?

My functions needs a file name so it can read and/or write to this file. 我的函数需要一个文件名,以便它可以读取和/或写入该文件。 I could use a simple string for this purpose 为此,我可以使用一个简单的字符串

void myFunc(string filename)

but I'd rather have some more type-safety by requiring a more specialized type and not detect that "" is no valid filename within the method. 但我宁愿通过要求更专业的类型而不是检测""在方法中没有有效的文件名而具有更多的类型安全性。

The class File doesn't work because it's static. File不起作用,因为它是静态的。

The class FileInfo could be used but I cannot remember that I've come across a filename argument of this type. 可以使用类FileInfo ,但我不记得我遇到过这种类型的文件名参数。

In personal projects I made up my own class that simply does some checking if the filename is well-formed etc. so eg an exception is thrown upon creation of the filename and not in the method itself. 在个人项目中,我编写了自己的类,只是检查文件名是否格式正确等等。例如,在创建文件名时抛出异常,而不是在方法本身。

Since the new project is of wider scope I'd thought it would be better to use a common standard to not confuse other users (or my future self in some weeks). 由于新项目范围更广,我认为最好使用通用标准,以免混淆其他用户(或者在未来几周内我的未来)。

Your thinking about FileInfo was correct - its only constructor has a fileName argument, and it performs basic validation for you. 您对FileInfo思考是正确的 - 它唯一的构造函数有一个fileName参数,它会为您执行基本验证。 From MSDN : 来自MSDN

public FileInfo(string fileName)

If you are finding FileInfo insufficient for some reason, you may be dealing with the file name in a manner that more closely matches a System.Uri . 如果由于某种原因发现FileInfo不足,您可能会以更接近System.Uri的方式处理文件名。 A Uri is not necessarily always related to a file, but a file path can be an example of a Uri, and Microsoft's implementation supports this with the IsFile property. Uri不一定总是与文件相关,但文件路径可以是Uri的示例,而Microsoft的实现通过IsFile属性支持它。 One could imagine a FileUri class which inherits from Uri and IsFile is forced. 可以想象一个强制继承Uri和IsFile的FileUri类。

It is probably worth mentioning that in most of the MSDN snippets I've been looking at, file paths and names are just passed as strings. 值得一提的是,在我一直关注的大多数MSDN片段中,文件路径和名称只是作为字符串传递的。 This may just be for convenience though. 这可能只是为了方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#数据结构:应该使用哪种类型的集合? - C# data structure: What type of collection should I use? 我应该使用哪种类型来使用C#保存图像? - What type should I use for saving images using C#? 我应该使用什么数据类型来表示 C# 中的钱? - What data type should I use to represent money in C#? 当我调用异步方法时,应使用哪种返回类型来表示成功? - When I call an Async method what return type should I use to indicate success? C#类型的尾随点表示什么? - What does the trailing dot on a C# type indicate? 我可以在类内创建C#方法吗?应该使用哪种类型的方法? - Can I create a C# method inside a class and what type of method should I use? C#开发人员使用什么约定来表明他们的类的作者? - What is the convention C# developers use to indicate their authorship of a class? 我应该使用哪种C#数据类型与非托管类型“ char *&sResult”进行交互 - What C# datatype should I use to interface with unmanaged type “char* &sResult” 我应该使用哪种类型的C#中的“类似数据库”的行为? - What type should i use for “database-like” behaviour in C#? 在C#中,应使用哪种数据类型存储具有标准偏差的数字列表? - In C#, what data type should I use to store a list of numbers with standard deviation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM