C# 中字符串的文件详细信息

发布于 2024-09-04 23:47:13 字数 232 浏览 1 评论 0原文

我在 C# 中有一个字符串,

String file="\\mserver-80\docs\somedoc.doc"

现在如何从上面的字符串中获取 fileInfo。 我的意思是, 我想声明类似的东西

FileInfo fInfo = new FileInfo(file);
fileExtn = fInfo.Extension;

I have a string in C#

String file="\\mserver-80\docs\somedoc.doc"

Now How do I get fileInfo from the above sting.
What I mean is,
I want to declare something like

FileInfo fInfo = new FileInfo(file);
fileExtn = fInfo.Extension;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-09-11 23:47:13

你也可以尝试

Path.GetExtension(file)

You can also try

Path.GetExtension(file)
自演自醉 2024-09-11 23:47:13

在 C# 中,字符串应该是

String file="\\\\mserver-80\\docs\\somedoc.doc";

您还可以使用 @ 字符转义字符串,这是一个更好的选择:

String file=@"\\mserver-80\docs\somedoc.doc";

除此之外,代码应该可以工作。

In C# the string should be

String file="\\\\mserver-80\\docs\\somedoc.doc";

You can also escacpe the string using the @ character, which is a better alternative:

String file=@"\\mserver-80\docs\somedoc.doc";

other than that the code should work.

娇俏 2024-09-11 23:47:13

使用 FileInfo类

只需添加

using System.IO;

但是,请注意 \ 必须转义为 \\
相反,您应该使用 @"" 字符串< /a>,像这样:

String file = @"\\mserver-80\docs\somedoc.doc"

That code will work fine, using the FileInfo class.

Simply add

using System.IO;

However, note that the \ must be escaped as \\.
Instead, you should use an @"" string, like this:

String file = @"\\mserver-80\docs\somedoc.doc"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文