小编spe*_*258的帖子

C# 获取文件大小

问题:我想获取文件大小,但文件没有扩展名 example testfile.txt是普通文件,这里是 example(不带引号)“ testfile ”,我也有没有扩展名但名称中有空格的文件 example(没有引号)“我的测试文件”,使用File.Exist读取为文件夹,当前函数我用于文件大小

    using System.IO;
    static long GetFileSize(string FilePath)
    {
        if(File.Exists(FilePath))
        {
            return new FileInfo(FilePath).Length;
        }
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

当前用法:

GetFileSize("C:\\Users\\TestUser\\Appdata\\Local\\Data Manager\\Super User\\User data controller");
Run Code Online (Sandbox Code Playgroud)

它抛出错误:System.IO.IOException: 'The directory name is invalid

文件不违反无效路径字符规则,路径只包含数字、字母和/或空格。

应用程序以管理员身份启动并且 Visual Studio 也以管理员身份启动

完整错误:

    System.IO.IOException
  HResult=0x8007010B
  Message=The directory name is invalid.

  Source=mscorlib
  StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.FileSystemEnumerableIterator`1.CommonInit()

   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)

   at System.IO.Directory.GetFiles(String path, …
Run Code Online (Sandbox Code Playgroud)

c# file

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

file ×1