p按段文件路径是否存在,通常的做法是使用

if(File.Exists(string path)){}

这个方法判断的原理是:打开path所指向的文件,如果文件能够打开,则文件存在;反之,文件不存在

可如果遇到大量的路径需要判断,且因为有的路径不存在或没有权限访问时,这样的方法会消耗大量时间,建议使用下面的方法

[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public extern static bool PathFileExists(string path);

        void Exists()
        {
            // A StringBuilder is required for interops calls that use strings
            StringBuilder builder = new StringBuilder();
            builder.Append(@"C:\test.txt");
            bool exists = PathFileExists(builder.ToString());
        }

 

相关文章:

  • 2021-12-07
  • 2022-12-23
  • 2021-09-26
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2021-07-02
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-11-19
  • 2021-07-01
  • 2022-12-23
相关资源
相似解决方案