Directory.SetCurrentDirectory throws PathTooLongException

霸气de小男生 提交于 2020-01-04 05:36:07

问题


There are several related questions on stackoverflow but either my situation is different or I am too dumb to relate those to situation. I am hoping someone can help me with this. Further I am not even much of a .NET developer so I apologize in advance for any wrong terminology use.

My scenario is as follows: The tool that is used to deploy our .net application (One Click?) puts it in a directory whose full name exceeds 300 characters. The application uses a third party component -- lets call it dbstore -- that processes the specified file that resides in the application deployment directory.

So far we were using Assembly.GetExecutingAssembly().GetName().CodeBase to construct the fully qualified name of the file to pass to dbstore. But dbstore uses old style APIs and fails when it tries to open the file.

Since dbstore is not expected to change soon, it was recommended that the application chdir to the deployment directory and pass a relative path name in current directory to it. This is also the approach described in the accepted response PathTooLongException in C# code

However I find that Directory.SetCurrentDirectory also throws PathTooLongException. This happens even when I am using UNC path name, e.g a name starting with \\?\0000000000000\...

Am I doing something fundamentally wrong? Is there another function to use?

EDIT: It seems there is no way to achieve what I am looking for. Far as I can tell there is no way to set current directory to a long path.


回答1:


Do you get a similar result when using Environment.SetCurrentDirectory() ?

If so, you may want to change your directory subfolder after subfolder.

EDIT:
Windows actually sets a limitation of 255 chars for a file path (WinXP) or 260 chars (Vista). Note that this limitation does not apply for the filesystem, so you can have a file stored in such a long directory path, but Windows Explorer and many Windows services cannot read from such path.

Actually it seems to also include .NET framework methods since you cannot access such files. You may need to write your own filesystem API, but that's a bit too much overhead. Can't you just shorten the file path ? Does Windows offer a shortened way to address a file (like 8 octet file names) ?

Source: http://labnol.blogspot.com/2006/10/limitations-with-long-file-names-on.html



来源:https://stackoverflow.com/questions/4050199/directory-setcurrentdirectory-throws-pathtoolongexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!