PathTooLongException在C#代码 [英] PathTooLongException in C# code

查看:125
本文介绍了PathTooLongException在C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 public static void Serialize()
    {

        List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
        List<string> dirFiles = new List<string>();
        foreach (string p in dirs)
        {
            string path = p;

            string lastAccessTime = File.GetLastAccessTime(path).ToString();


            bool DirFile = File.Exists(path);
            FileInfo fInf = new FileInfo(path);
            long lengthInk = fInf.Length / 1024;

            DateTime lastWriteTime = File.GetLastWriteTime(p);
            dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + lengthInk.ToString() + " kb");


        }



我一直打同一个PathTooLongException错误以下行:

I keep hitting a PathTooLongException error with the following line:

string lastAccessTime = File.GetLastAccessTime(path).ToString();

应用程序演练成一个驱动器,并找到所有文件/文件夹瓦特/驱动器。我不能改变这条道路,但因为它是上面260个字符......如何解决此问题?

The application drills into a drive and finds all files/folders w/in the drive. I cannot change this path but since it is above 260 characters...how to work around this?

推荐答案

GetLastAccessTime()电话,一个完整的路径可< A HREF =htt​​p://msdn.microsoft.com/en-us/library/system.io.pathtoolongexception.aspx相对=nofollow>超出内部限制(这是操作系统特定版本, 。但通常260个字符),对一个完全合格的文件路径的最大长度

The GetLastAccessTime() call, with a full path can exceed the internal limit (which is OS-version specific, but typically 260 characters) on the maximum length for a fully qualified file path.

要避免这种情况的一种方法,是使用的 Directory.SetCurrentDirectory() 来改变当前的系统目录,然后调用 GetLastAccessTime()只有一个相对路径。只要确保你改变你的当前目录回到你开始从以避免意外的问题。

One way to avoid this, is to use Directory.SetCurrentDirectory() to change the current system directory and then call GetLastAccessTime() with only a relative path. Just make sure you change your current directory back to what you started from to avoid unexpected issues.

这篇关于PathTooLongException在C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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