获取文件格式 [英] Getting the format of a file

查看:107
本文介绍了获取文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是VB的新手,但我还是决定构建一个强悍的程序,该程序将教我很多东西,不幸的是,我被困在>〜<
这是我的代码的一部分:

hello im kind of newbie in VB however ive decided to build tough program that will teach me alot, Unfortunetly I''m stuck >~<
here''s a part of my code:

    Try
        Dim foundFile As String = My.Computer.FileSystem.GetFiles(filename).ToString()
        Dim check As String = System.IO.Path.GetExtension(foundFile)
        Return check
    Catch ex As ArgumentException
    End Try
End Function


这部分负责将扩展名从文件名中删除

在此部分:


this part is responsable of saparting the extension out of the File''s name

At this part:

Dim foundFile As String = My.Computer.FileSystem.GetFiles(filename).ToString()
Dim check As String = System.IO.Path.GetExtension(foundFile)



我正在通过用户代码来理解IOException:

预先谢谢你,tsahi.

[edit]删除了虚假标签-OriginalGriff [/edit]



I''m getting IOException was unhendaled by user code:

Thank you in advance, tsahi.

[edit]Spurious tags removed - OriginalGriff[/edit]

推荐答案

选中此选项..

如何从完整文件路径获取文件名 [ ^ ]
check this..

How to get File Name from Full file Path[^]


如果您查看文档(
If you look at the documentation (MSDN[^]) you will see:
The following conditions may cause an exception:

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\) (ArgumentException).

The path is not valid because it is Nothing (ArgumentNullException).

directory does not exist (DirectoryNotFoundException).

directory points to an existing file (IOException).

The path exceeds the system-defined maximum length (PathTooLongException).

A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).

The user lacks necessary permissions to view the path (SecurityException).

The user lacks necessary permissions (UnauthorizedAccessException).

由于您收到IOException,因此问题非常明显.

解决方案:当只需要路径时,不要给它一个文件名...

Since you are getting an IOException, the problem becomes pretty obvious.

Solution: Don''t give it a file name when it expects just the path...


GetFiles(path)返回您提供的路径中所有文件,该文件为string数组价值观. ToString()将返回Array.ToString,而不是所需的文件名.

只需使用以下内容即可获得所需的内容:
GetFiles(path) returns all files within the path you supplied as an array of string values. ToString() will return Array.ToString and not the filename you want.

Just use the following to get what you want :
  ...
  Dim check As String = System.IO.Path.GetExtension(filename)
  Return check
End Function


这篇关于获取文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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