stefanto70
stefanto70

Reputation: 41

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

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.

Upvotes: 4

Views: 30468

Answers (2)

drzaus
drzaus

Reputation: 24994

To expand on @TheSolution's answer:

  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 using section at the top of the code files which use the common code.

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

  1. Project > Properties (ALT+ENTER on project in Solution Explorer)
  2. Change both "Assembly name" and "Default namespace"
  3. Close VS (or unload solution)
  4. Rename folder
  5. Open .sln file in a text-editor and follow the manual steps below
  6. Open the .csproj files of all other projects referencing this one and change references from "MyProject" to "YourProject". Alternatively, delete and re-add project references within VS.

.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:

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>

Upvotes: 4

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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 hacky way 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.

Upvotes: 1

Related Questions