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

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.

To expand on #TheSolution's answer:
File > New Project (or Solution > Right-click > Add New Project) > Class Library -- name it something like "MyProject.Common"
Add Existing Files (Shift+Alt+A) to project > choose files to share. Or copy/paste
Delete old files in original project, as they've been copied to the shared project
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"
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":
Project > Properties (ALT+ENTER on project in Solution Explorer)
Change both "Assembly name" and "Default namespace"
Close VS (or unload solution)
Rename folder
Open .sln file in a text-editor and follow the manual steps below
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>

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.

Related

Reference code files from external folder

I try to reference a collection of c# code to a console project in visual studio.
I want to keep the referenced code outside the console project folder.
I want the code to automatically update when it gets changed in the external position.
The referenced codes folder structure should stay intact. (~100 files)
Is there a way of referencing/linking the code without updating everytime? "Include in Project" only works if the code is inside a solution folder.
You can add a "link" to code files outside of your project. This doesn't make a copy of the files.
Right-click your project -> Add -> Existing Item..., and browse to the file(s). Then click the down-arrow on the "Add" button and select "Add As Link":
Linked files appear with a blue arrow in the Solution Explorer:
If you want to reference an entire folder structure of code, you'll need to edit your .csproj. Something like:
<ItemGroup>
<Compile Include="..\SomeDir\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)"/>
</ItemGroup>
Adjust ..\SomeDir to be the path to your code. The **\*.cs is of course a pattern to recursively include all .cs files. %(RecursiveDir), %(Filename), and %(Extension) are MSBuild placeholders.
I organise my git repos in a flat structure:
core
shared1
website1
website2
Where websites 1 and 2 both reference core and shared1.
When I add project references to core and shared1 from websites 1 and 2, they remain intact because they are both in the same relative location from the point of view of the websites.
Additionally, I organise my solution to mirror the external repo configuration, e.g.
core (solution folder)
corelibrary.csproj
shared1 (solution folder)
sharedlibrary.csproj
website1services.csproj
website1.csproj
I found a good solution, but I am curious if there is a better one. I am on Windows and using a simbolic link
cmd /c mklink /d /j _LinkedCode ..\..\..\_Code
then I can use "Include in Project" in visual studio

Files do not show up in solution explorer, even when added to project

I inherited a codebase, and one of the projects has a few hundred c# files, but in solution explorer it only shows a couple. These classes are referenced in other projects, and the solution builds fine.
If I click the "Show All files" option, it will show the obj and bin folders and a config file, but none of the .cs files that exist in that folder show up there. If I go to project -> Add Existing Item and select one of the files, the project does not change. All the files I'm looking for exist in the appropriate folder.
Why are the files missing in solution explorer, but still referenced in the codebase? How do I get them to show up in solution explorer?

Refactor on multi project in Visual Studio with lots of "Linked Files"

I have several C# project in different solution of Visual Studio.
There are many .cs file that are referenced from different project (NOT copy of same .cs file, but only 1 file and several reference in several project).
Now I have created a general solution (.sln) where are imported ALL my project (.csproject)
What I need is refactor some variable name in ALL my project.
I try to open in visual studio the .cs file and do a refactor but it change all name of that variable only inside THAT project and NOT of all project! And if I mouve inside other project i found the error because that variable is not find and I can't do refactor in the same way...
So, what I can do?
I give some other info:
All project compile separatly in each separate solution.
In general solution I import all *.csproj and if i build all project simultaneously or separately they correctly build.
all .cs files that are referenced in many project were added by link and NOT copied
If I open one common .cs file in one project (.csproj) and try to open the same .cs inside another project (.csproj) visual studio give me the error "the file is already opened in another project of solution"
maybe the most important thing: If I open one common .cs file in one project I can navigate inside his functions... In visual studio upper the function there is a small menu that tell ALL reference of that function inside the project.
OK, in this menu I see only the reference inside the project where i open the file and NOT the reference of the other project of my solution.
example: in file "utility.cs" I have function named "TryConvertMatrix(..)". I have one project named "river" that use this function and onother project named "telephone" that use this function. In both project the file "utility.cs" was added as link. If I open file "utility.cs" from project "river" I see all reference of the function "TryConvertMatrix(..)" inside the .cs files of project "river" but NOT where was called inside .cs file of "telephone" project
In the example explaned before, if I try to refactor the name of function "TryConvertMatrix(..)" in "ConvertPerfectMatrix(..)" Visual Studio, correctly, change all reference ONLY inside "river" project!
So when I navigate inside the project "telephone" I find all calling to "TryConvertMatrix(..)" instead the new name so it can't build! And, in this case, I can't do a refactor in this project because... No build ---> No refactor
What I need is only change a name of a variable and ALL reference in ALL project that I have opened in my solution.
I try do delete all project's bin folder, restart visual studio and rebulild
In this way no improvement...
Many thanks,
Massimiliano

Reference a resource to be copied at build time

In Visual Studio, I can add a resource (text file, image, etc.) to the project and have it copy to the output directory at build time. The problem with this is that the project copies the file into the project directory. I have a multi-project solution where many projects are referencing the same resource, I don't want the resource copied into every project directory. I want the resource to stay in a separate resource directory that I have under source control with the rest of the solution. Is there a way for me to add the resource to the projects and have the projects copy the resource to the debug and release directories on build that wouldn't force the file into the project directory?
In project explorer select the folder or project where you want to add the file and right click. Select Add Existing Item from menu.
Locate the file and click on the arrow near the Add button. From the menu opened, select Add As Link.
Do not forget to change the properties of the linked item (Copy To Output directory etc).
You should be able to link the files into your projects instead of copying them. See http://support.microsoft.com/kb/306234 for steps.

Changing The Name Of A C# Project Folder?

I have a main project directory with the following contents:
SubDirectory (Directory)
Project.sln (Solution File)
When I try manually changing SubDirectory's name in my Windows explorer, I get errors when I open the solution file. How can I rename this directory without affecting my solution?
Thanks!
Rename it in Visual studio, or edit the .csproj/.sln file with a text editor and fix the refrences.
Open the solution file in a text editor and rename all references to the old folder.
Open up the .sln file in notepad. You'll see a line like this:
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "myproject", "myproject\myproject.csproj", "{DF81752F-37EE-4F4E-BC22-B09C8D05ED78}"
EndProject
If you want to rename the folder from myproject to newproject, you can change myproject\myproject.csproj to newproject\myproject.csproj (or whatever).
Change the name in SolutionExplorer.. it will work fine..
However it doesn't gurantee problem with Source Control or TFS you will need to resolve them manually
Your solution has the paths to your project files embedded in it, which includes the folder name. If you want to rename the folder, you have three options:
Close the solution. Rename the folder, then reopen the solution. The projects will show up as gray, and you'll need to click each one and locate the project file in the Properties window. Note that this may remove project references, but it may not. This is what I would suggest.
Open the solution and remove the project(s) within that directory. Note that this will remove any project references from any other projects that reference the project(s) that you're removing. Rename the folder, then add the project (and any project references) back.
Close the solution. Rename the folder, then open the .sln file in Notepad (or some other text editor) and fix the paths manually. This will preserve any project references.
When I have done it, I manually change it, then open the solution. You will get an error about not being able to find the project file, just choose to locate it, and it will re-map it. If you are using VSS, make sure everything is checked in first.
There are probably paths in project/solution related files that contain the old directory name. You'll either have to update those manually or find a way to rename the project from within Visual Studio.
This might help you rename the project:
http://msdn.microsoft.com/en-us/library/3e92t91t%28v=VS.90%29.aspx
In Visual Studio solution explorer,
edit the project properties to change
the assembly name, namespaces etc.
etc. to what you want.
Rename the top
Project nodes in VS solution explorer.
Shut Visual Studio down.
Open Windows explorer and rename your
folders and .csproj files to what you
want.
With a text editor and NOT Visual
Studio, open up the
sln file,
the .csproj files and
anything else you've renamed.
do a
Find & Replace looking for the old project strings, filenames,
namespaces (if required - I suggest
you leave that bit for when all has
been transitioned) and replacing them
as required.
I'm suggesting you'll
need to be selective because I don't
know how you've named your projects
and .csproj files ;-)
Once you've done all that, quit your
text editor and try opening up your
.sln file again.
Do a Rebuild the first time to remove any artifacts from the old configuration/naming.
HTH

Categories