在 Visual Studio 2012 中的 C# 项目中更改文件完整路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15901894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:23:01  来源:igfitidea点击:

Change file full path in a C# project in Visual studio 2012

c#.netvisual-studio

提问by stefanto70

In your opinion , it would be possible to change a file full path in a C# project, In Visual Studio 2012?? I ask this because in visualization, the properties box for a project file that contains full path appears as disable ( gray). I would use the same file in different project, under the same solution, without duplicate file code .cs.

在您看来,可以在 Visual Studio 2012 中更改 C# 项目中的文件完整路径?我问这个是因为在可视化中,包含完整路径的项目文件的属性框显示为禁用(灰色)。我会在不同的项目中使用相同的文件,在相同的解决方案下,没有重复的文件代码 .cs。

回答by Mike Perrenoud

If you wanted to share a code file then you could add it as a Linked File. You can do this by right clicking on the project, selecting add existing item, finding the item in the open file dialog, and then before selecting it drop down the Open button and select the option for linked.

如果您想共享代码文件,则可以将其添加为链接文件。您可以通过右键单击项目,选择添加现有项目,在打开文件对话框中找到项目,然后在选择它之前下拉打开按钮并选择链接选项来完成此操作。

This will now link that code file to this project - or in other words - when it's changed in one place it's changed in both.

现在,这会将代码文件链接到该项目——或者换句话说——当它在一个地方发生变化时,它在两个地方都发生了变化。

However, linked files are a very hackyway of providing shared functionality. The more appropriate approach is to build a Class Library with the functionality and add the DLL as a reference to the project. This means that you'll need to have a way of keeping that DLL up to date - but it's still generally more appropriate.

但是,链接文件是提供共享功能的一种非常笨拙的方式。更合适的方法是构建具有该功能的类库并将 DLL 添加为对项目的引用。这意味着您需要有一种方法来使该 DLL 保持最新状态 - 但它通常仍然更合适。

回答by drzaus

To expand on @TheSolution's answer:

扩展@TheSolution的回答

  1. File > New Project (or Solution > Right-click > Add New Project) > Class Library -- name it something like "MyProject.Common"
  2. Add Existing Files (Shift+Alt+A) to project > choose files to share. Or copy/paste
  3. Delete old files in original project, as they've been copied to the shared project
  4. Then in the projects in which you want to use the common files, Project > Add Reference> Choose from under "Solution > Projects" in left panel, check the "Common" project and choose "OK"
  5. Include the namespace in the usingsection at the top of the code files which use the common code.
  1. 文件 > 新建项目(或解决方案 > 右键单击​​ > 添加新项目)> 类库——将其命名为“MyProject.Common”
  2. 将现有文件 ( Shift+Alt+A)添加到项目 > 选择要共享的文件。或复制/粘贴
  3. 删除原始项目中的旧文件,因为它们已被复制到共享项目中
  4. 然后在要使用公共文件的项目中项目>添加引用>从左侧面板的“解决方案>项目”下选择,选中“公共”项目并选择“确定”
  5. using使用公共代码的代码文件顶部的部分中包含命名空间。


If you're talking about renaming a C# project and its folder, you have to do this manually in the .slnand .csprojfiles after renaming the project in the solution. ex) If you want to rename "ME.MyProject" to "ME.YourProject":

如果您要重命名 C# 项目及其文件夹,则必须在解决方案中重命名项目后在.sln.csproj文件中手动执行此操作。例如)如果您想将“ME.MyProject”重命名为“ME.YourProject”:

  1. Project > Properties (ALT+ENTERon project in Solution Explorer)
  2. Change both "Assembly name" and "Default namespace"
  3. Close VS (or unload solution)
  4. Rename folder
  5. Open .slnfile in a text-editor and follow the manual steps below
  6. Open the .csprojfiles of all other projects referencing this one and change references from "MyProject" to "YourProject". Alternatively, delete and re-add project references within VS.
  1. 项目 > 属性(ALT+ENTER在解决方案资源管理器中的项目上)
  2. 更改“程序集名称”和“默认命名空间”
  3. 关闭VS(或卸载解决方案)
  4. 重命名文件夹
  5. .sln在文本编辑器中打开文件并按照以下手动步骤操作
  6. 打开.csproj引用此项目的所有其他项目的文件,并将引用从“MyProject”更改为“YourProject”。或者,在 VS 中删除并重新添加项目引用。

.sln:

.sln

BEFORE:

前:

Note that the project file has been renamed, but not the path.

请注意,项目文件已重命名,但路径未重命名。

Project("{SOMEGUID}") = "ME.YourProject", "ME.MyProject\ME.YourProject.csproj", "{ANOTHERGUID}"
EndProject

AFTER:

后:

Project("{SOMEGUID}") = "ME.YourProject", "ME.YourProject\ME.YourProject.csproj", "{ANOTHERGUID}"
EndProject

.csproj:

.csproj

BEFORE:

前:

<ProjectReference Include="..\ME.MyProject\ME.YourProject.csproj">
  <Project>{ANOTHERGUID}</Project>
  <Name>ME.YourProject</Name>
</ProjectReference>

AFTER:

后:

<ProjectReference Include="..\ME.YourProject\ME.YourProject.csproj">
  <Project>{ANOTHERGUID}</Project>
  <Name>ME.YourProject</Name>
</ProjectReference>