Visual Studio - PathTooLongException even when the path length is less than 260 characters

烈酒焚心 提交于 2019-12-10 10:24:15

问题


I know that max path length allowed while creating a project is 260 characters and 248 characters for directory. But I'm getting this error even when my my path length is less than 200 characters.

My solution file resides in this folder C:\Users\username\Documents\Visual Studio 2010\Projects\Health12.Domain12.WardManagement => 88 characters When I try to add a new class library with name "Health12.Domain12.WardManagement.Service.Contract" to this solution, it gives me path too long exception.

C:\Users\username\Documents\Visual Studio 2010\Projects\Health12.Domain12.WardManagement\Health12.Domain12.WardManagement.Service.Contract => 138 characters

C:\Users\username\Documents\Visual Studio 2010\Projects\Health12.Domain12.WardManagement\Health12.Domain12.WardManagement.Service.Contract\Health12.Domain12.WardManagement.Service.Contract.csproj => 195 characters

None of the possible files or folders crosses the 260 character limit.

If i change the project name from "Health12.Domain12.WardManagement.Service.Contract" to "Health12.Domain12.WardManagement.Service.Con", it works fine. Any idea why visual studio won't allow to create project even when it is not crossing max allowed length limit.


回答1:


The length of the path in Visual Studio is computed as follow:

  1. Path to solution: C:\Users\username\Documents\Visual Studio 2010\Projects\Health12.Domain12.WardManagement\ = 89 chars
  2. 2 * Project name : Health12.Domain12.WardManagement.Service.Contract\ = 50 chars
  3. Reserved chars : Additionnal characters for the files in the project, the compiled files, temporary files, ... = 80 chars

In your case: 89 + 2*50 + 80 = 269

The limit is 259 ((Drive char):\(256-character path)) (see Naming Files, Paths, and Namespaces)

You have to remove 10 chars. Either you reduce the length of the path to the project by 10 chars or you remove 5 chars in the project name ("tract" for example works).




回答2:


This limit is part of the Windows Kernel and is hardly solvable. I found a workaround that ALLOW working with path with more than 260 chars.

Disclaimer: I've tried this trick only on Windows 8 x64 and Visual Studio 2013

So, to make it work I've just create a junction to the folder with the mklink command:

Assume this is the original path: d:\very\very\long\path\to\solution\folder, you can obtain a short link as d:\short_path_to_solution_folder just jaunching this command from a dos shell as administrator:

mklink /J d:\short_path_to_solution_folder d:\very\very\long\path\to\solution\folder

change source and destination path to meet you needs.

Best Regards! Stelvio



来源:https://stackoverflow.com/questions/17807281/visual-studio-pathtoolongexception-even-when-the-path-length-is-less-than-260

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