9

This is pretty crazy, I am only adding an existing .h file to VS2015 C++ project but it would complain:

The desired name for c:\code\usbview\USBdevices.h is invalid

I was able to add this file to project when it had its own .sln file. However, I created a new empty .sln file and wanted to add this and other project and that's how this issue kicked in. It wouldn't add this project to new solution giving a similar weird error about the files I added.

I then add the base project to that solution file and tried to add this .h and .cpp files there but it adds the .cpp but gives this error for .h file.

This is very weird, I changed the files names but the same error.

zar
  • 11,361
  • 14
  • 96
  • 178

6 Answers6

2

The link in the comment section by BlackDwarf fixed the problem even though that was was for C# but applies.

I didn't have USERPROFILE environment variable so I created it and set it to c:\Users\my.name and now I can add the file

Community
  • 1
  • 1
zar
  • 11,361
  • 14
  • 96
  • 178
2

I had the same problem and it was because of merge errors in the file ProjectName.vcxproj.filters.

habakuk
  • 2,712
  • 2
  • 28
  • 47
1

The answers here led me to check my vcxproj when I experienced the OP's problem. It turns out I had two filters with the same name in my Project.vcxproj.filters, something like

<Project>
  <Common>
  ...
  ...
  <Stuff>
     <Common>

And I was trying to add files to the second Common.

To fix it, I had to ..

  • edit the vcxproj manually in {favoured text editor} and remove the second filter I'd added with a duplicate name.
  • Unload/Reload the project from the solution in VS(2017), and add a filter with a unique name. All good - I can add files to my new filter.
coolhandle01
  • 81
  • 14
1

I use VS 2013 Express and had the same error.

Solved by removing VCTargetsPath environment variable, I added it some time ago, don't remember why :)

Dmytro
  • 1,290
  • 17
  • 21
1

I had the same issue, but the solution had nothing to do with "Environment Variables" for me. What I did: Close Visual Studio 2017. Reopen my project. Suddenly I had two copies of the "Helper.cpp" and "Helper.h" that I was trying to add already as part of the solution files. Clicking on each I saw that one of the files was invalid, probably because I had deleted that version of the file in that file location. I deleted the invalid files, checked to make sure that the files had the correct code I wanted in them, built the solution and everything ran fine.

Not sure why closing and reopening VS fixed the problem, but glad it did.

Adam H.
  • 19
  • 4
  • Opening and closing Visual Studio 2017 worked for me. On re-opening I discovered that my attempt to add a .resx file to a filter called "Resource Files" had succeeded. – eklektek Jun 22 '21 at 07:06
0

If the environment variable route doesn't work for you, check the integrity of your project file next.

I had this problem in 2017, and it was due to a double entry in the vcxproj from a manual editing error. Our vcxproj file had the following in it:

<ClInclude Include="XXXX.h" /> <ClCompile Include="XXXX.h" />

In 2017 we got exactly the error you described when trying to add an existing file, a slightly different one when adding new, and strange crashes elsewhere. Opening the solution in 2015 logged the exact problem and failed to load the project. We removed the double entry and all the mysterious problems vanished.

John Neuhaus
  • 1,784
  • 20
  • 32