简体   繁体   English

如何在Visual Studio 2010中恢复损坏的.csproj文件?

[英]How can I recover a corrupt .csproj file in Visual Studio 2010?

My PC 我的电脑
Windows 8 Preview Release Windows 8预览版
Visual Studio 2010 Express for Windows Phone 适用于Windows Phone的Visual Studio 2010 Express

My App is in 我的应用程序在
Solution A 解决方案A.
Project B 项目B.

Story 故事
The screen is freezing when I type codes 我键入代码时屏幕冻结
I power off the PC then restart 我关闭电脑然后重新启动
When I load the sln in VS2010 for wp 当我在VS2010中为wp加载sln时
VS2010 said VS2010说

"Solution A (0 projects) “解决方案A(0个项目)
Project B (unavailable) 项目B(不可用)
The project file cannot be loaded 无法加载项目文件

So I tried to "Edit B.csproj" in VS2010, it shows error 所以我试图在VS2010中“编辑B.csproj”,它显示错误

B.csproj : error : Unable to read the project file 'B.csproj'. B.csproj:错误:无法读取项目文件'B.csproj'。
B.csproj: The project file could not be loaded. B.csproj:无法加载项目文件。 Root element is missing. 根元素缺失。

Help 救命
How can I open my solution correctly? 如何正确打开我的解决方案? :( :(

The easiest thing to do is probably to create a new project file. 最简单的方法是创建一个新的项目文件。 The simplest way to do this would be to: 最简单的方法是:

  1. Open a fresh copy of Visual Studio 打开Visual Studio的新副本
  2. Create a project in a different location of the same type as B , with the same name. B相同类型的不同位置创建项目名称相同。
  3. Close that copy of Visual Studio 关闭Visual Studio的副本
  4. Copy the newly created B.csproj file to the directory where all your source for B is, overwriting the existing B.csproj file (but take a copy of it, just incase you need it!). 将新创建的B.csproj文件复制到B所有源代码所在的目录,覆盖现有的B.csproj文件(但需要复制它,只需要它就可以了!)。
  5. Load Visual Studio and open Solution A . 加载Visual Studio并打开解决方案A

You'll note that the project doesn't contain any of the files / references you'd expect and will probably contain a "broken" reference to Class1.cs or any other files that were added to the new project file when Visual Studio created it. 您会注意到该项目不包含您期望的任何文件/引用,并且可能包含对Class1.cs的“损坏”引用或Visual Studio创建时添加到新项目文件的任何其他文件它。 Now: 现在:

  1. Make sure Project B is selected in Solution Explorer and click the "Show All Files" icon. 确保在解决方案资源管理器中选择了项目B,然后单击“显示所有文件”图标。 It's the one that has a picture of 3 files overlaying each other in it, to the left of the Refresh icon. 它是一个在刷新图标左侧有3个文件相互叠加的图片。
  2. You'll be able to see all the content of your project but "greyed out". 您将能够看到项目的所有内容,但“灰显”。 Select each file/folder you want to add back, right click and choose "Include In Project". 选择要添加的每个文件/文件夹,右键单击并选择“包含在项目中”。

Assuming there's nothing too complicated about your project, you should only now need to re-add any References and it should compile and run. 假设您的项目没有太复杂,您现在应该只需要重新添加任何引用,它应该编译并运行。 Any further things you can probably work out by using a tool like Beyond Compare to compare the new project file and the original project file to look for things you've missed. 通过使用Beyond Compare之类的工具来比较新项目文件和原始项目文件以查找您错过的内容,可以解决更多问题。

Most important of all: Start using Source Control! 最重要的是:开始使用Source Control!

If you were, you'd be able to recover a previous version of your project file and not have to go through this hassle. 如果你是,你将能够恢复以前版本的项目文件,而不必经历这个麻烦。 Pretty much any source control solution you can think of can be run on a PC without requiring a dedicated server. 几乎所有您能想到的源控制解决方案都可以在PC上运行而无需专用服务器。 One of the easiest to setup is VisualSVN Server , which is free for personal and commercial use. 最容易设置的是VisualSVN Server ,它可以免费用于个人商业用途。

Just delete the .user file in the VS folder. 只需删除VS文件夹中的.user文件即可。 source: http://msbiztalk.blogspot.in/2010/10/visual-studio-error-imported-project.html 来源: http//msbiztalk.blogspot.in/2010/10/visual-studio-error-imported-project.html

Provided you do not have a large number of source files, the fastest way is probably to just delete the .csproj and create a new one. 如果您没有大量源文件,最快的方法可能是删除.csproj并创建一个新文件。 Then add all the source code back in manually. 然后手动添加所有源代码。 If you create the project file in the same location you can check the "Show All Files" in the Solution Explorer and then right click on the source files and select "Include in Project". 如果在同一位置创建项目文件,则可以在解决方案资源管理器中选中“显示所有文件”,然后右键单击源文件并选择“包含在项目中”。

Just complementing what @Rob said. 只是补充@Rob所说的内容。 You should give a try to Git as a Source Control. 你应该尝试将Git作为源代码控制。 You can make local commits and if you need a remote server there are GitHub and BitBucket (free private repositories) as two good options. 您可以进行本地提交,如果您需要远程服务器,则有GitHub和BitBucket(免费私有存储库)作为两个不错的选择。

Take a look here: http://git-scm.com/book/en/Getting-Started 看看这里: http//git-scm.com/book/en/Getting-Started

对我有用的是删除文件并粘贴同一文件的备份,然后在解决方案中重新加载项目。

This worked for me:- 这对我有用: -

I had saved the copy of the project at different location too on my system.I just replace the project.csproj file from that saved copy to my this project. 我在我的系统上也在不同的位置保存了项目的副本。我只是将project.csproj文件从保存的副本替换到我的这个项目。 I opened the file .csproj of my project and found that it was empty. 我打开了我项目的.csproj文件,发现它是空的。 So I replaced this with the saved copy of the same project. 所以我将其替换为同一项目的保存副本。 Close the visual studio and opened the project again. 关闭视觉工作室并再次打开项目。 And now I found all the files being loaded. 现在我发现所有文件都被加载了。

Hope this help others too. 希望这也能帮到别人。

In Visual Studio: 在Visual Studio中:

  1. Right click on the project and select Unload Project ; 右键单击项目并选择Unload Project ;
  2. Right click on the project again (now displayed as "unavailable") and select Edit MyProject.csproj ; 再次右键单击项目(现在显示为“不可用”)并选择Edit MyProject.csproj ;
  3. Fix the errors underlined in red; 修复红色下划线的错误;
  4. Right click on the project again and select Reload Project . 再次右键单击该项目,然后选择Reload Project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM