Visual Studio 变得疯狂:“目录名称无效”尝试编译时出错

发布于 2024-09-12 06:59:04 字数 522 浏览 2 评论 0原文

由于一些非常奇怪的原因,我的 Visual Studio 2008 在尝试编译 C# 项目时,尝试将可执行文件的输出写入与可执行文件同名的目录,至少看起来这就是错误消息所暗示的内容。在我的任何项目上运行 Compile 后,CSC.EXE 报告以下编译器错误:

无法写入输出文件 'D:\Projects\Examples\StringBuilderVsString\obj\Release\StringBuilderVsString.exe' -- '目录名无效。 '

当我查看 obj\Releaseobj\Debug 内部时,所有中间资源(例如 StringBuilderVsString.pdb)都正确写入该目录中。目录中没有空格。从命令提示符运行 csc.exe 时也会发生该错误。

运行进程监视器时,我只看到 csc.exe 访问的文件成功,并且没有看到任何查询或写入不存在的位置。

欢迎任何解决这个问题的想法。

For some very odd reason, my Visual Studio 2008, when trying to compile a C# project, tries to write the output of the executable to a directory with the same name as an executable, at least it seems that that's what the error message implies. After running Compile on any of my projects, CSC.EXE reports the following compiler error:

Could not write to output file
'D:\Projects\Examples\StringBuilderVsString\obj\Release\StringBuilderVsString.exe'
-- 'The directory name is invalid. '

When I look inside obj\Release or obj\Debug all intermediate resources, like StringBuilderVsString.pdb are correctly written in that directory. There are no spaces in the directory. The error happens also when running csc.exe from the command prompt.

When running Process Monitor, I only see SUCCESS for files accessed by csc.exe and I don't see any querying or writing to locations that do no exist.

Any ideas on tackling this are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诺曦 2024-09-19 06:59:04

解决了!感谢您的意见,我同时找到了解决方案。原因很少见,但可能会发生在其他人身上,所以我决定在这里回答我自己的问题:

原因

错误或无法访问的 %TEMP% 位置,或无法访问的 %USERPROFILE%路径。

我已将这些路径移至更快的 SSD 驱动器以加快编译速度。驱动器崩溃并且操作系统不再找到它(据报道 在 ServerFault 上),因此,移动的位置(互联网缓存、我的文档和临时文件)变得无法访问。

Visual Studio 提到的错误具有误导性:它与文件名无关(虽然我仍然想知道为什么它以目录形式查询可执行文件),但错误与 %TEMP% 位置错误有关。

解决方案

修复 %USERPROFILE%%TEMP% 环境变量(如果您忘记了,它位于:系统 > 高级 > 环境变量)。

注意:典型的默认 TEMP 设置是使用 USERPROFILE 环境变量,如默认 %USERPROFILE%\AppData\Local\Temp 中所示。此方法有效,但前提是在注册表中 HKCU\Environment\TEMP 下的项的类型为 REG_EXPAND_SZ 而不是 REG_SZ

Solved! Thanks for the input, I meanwhile found a solution. The cause is rare, but it may happen to others, so I decided to answer my own question here:

Cause

An incorrect or inaccessible %TEMP% location, or an inaccessible %USERPROFILE% path.

I had moved these paths to a faster SSD drive to speed up compiling. The drive crashed and the OS did not find it anymore (as reported on ServerFault here) and consequently, the moved locations (internet caches, my documents and temp) had become inaccessible.

The error mentioned by Visual Studio is misleading: it has nothing to do with the name of the file (while I'm still wondering why it was querying the executable as a directory), the error was about the %TEMP% location being bad.

Solution

Fix the %USERPROFILE% or %TEMP% environment variables, (in case you forgot, it's here: System > Advanced > Environment Variables).

Note: The typical and default TEMP setting is to use the USERPROFILE environment variable as in the default %USERPROFILE%\AppData\Local\Temp. This works, but only if in the registry, the key under HKCU\Environment\TEMP is of the type REG_EXPAND_SZ and not REG_SZ.

埖埖迣鎅 2024-09-19 06:59:04

确保驱动器中有足够的可用空间,我也遇到过同样的问题,分配更多可用空间后解决了。

Make sure that you have enough free space in your drive, I have experienced the same issue, got it resolved after allocating more free space.

娇纵 2024-09-19 06:59:04

就我而言,当我尝试将文件 (Database.mdf) 添加到文件夹 (App_Data) 时,就会发生这种情况。

通过在添加文件 (Database.mdf) 之前手动添加文件夹 (App_Data) 来解决此

问题当 .sln 注册一个文件夹(这就是它在 Visual Studio 中显示的原因)而该文件夹实际上并不存在于磁盘上时,会发生这种情况。

In my case, this happens when I try to add a file (Database.mdf) to a folder (App_Data).

Solve it by adding the folder (App_Data) manually before adding the file (Database.mdf)

This happens when .sln registering a folder (that's why it shows up in Visual Studio) while that folder does not actually exist on disk.

此岸叶落 2024-09-19 06:59:04

我也遇到了类似的错误无法写入输出文件'........................'--
'目录名称无效。 '

我尝试删除文件夹,按照上面的建议更改环境变量(即%USERPROFILE%或%TEMP%),但似乎没有任何效果。
当我再次查看 VS 2008 中的输出窗格/窗口中的输出时,我发现了以下行

“c:\ Windows \ Microsoft.NET \ Framework \ v3.5 \ Microsoft.Common.targets(3397,13):
错误 MSB6003:无法执行指定的任务可执行文件“cmd.exe”
跑步。找不到目录路径:
C:\Users\Administrator\AppData\Local\Temp\2\"

这一行引导我找到了解决方案。
我刚刚在“C:\Users\Administrator\AppData\Local\Temp”文件夹中创建了不存在的 2 文件夹。

%USERPROFILE% = C:\Users\Administrator\AppData\Local\Temp

顺便说一句,我们仅在所有云实例上注意到这一点。与虚拟化有关吗? ..真的不知道

I was also getting a similar error Could not write to output file '..........................' --
'The directory name is invalid. '

I tried deleting folder, changing Environment variables as suggested above (i.e. %USERPROFILE% or %TEMP%), but nothing seemed to work.
When I again looked at the output in the Output Pane/Window in VS 2008, I found the following line

"c:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(3397,13):
error MSB6003: The specified task executable "cmd.exe" could not be
run. Could not find directory path:
C:\Users\Administrator\AppData\Local\Temp\2\"

This line led me to the solution.
I just created the non existing 2 folder in "C:\Users\Administrator\AppData\Local\Temp" folder.

%USERPROFILE% = C:\Users\Administrator\AppData\Local\Temp

By the way we noticed this only on all of our Cloud instances. Something do with virualization? .. Dont know really

一瞬间的火花 2024-09-19 06:59:04

将该文件移动到其他地方,然后尝试运行它。它对我有用。

Move that file to somewhere else and then try running it.It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文