Pregunta

My Current Situation:

I had a solution in which I split a large MVC project into two smaller projects. Of course the two smaller projects reference the same JavaScript and content files that were in the old larger project.

What I wish to do:

Instead of duplicating the scripts and content in the 2 projects, I want to create a shared folder that the 2 project refer to using something like a link or a shortcut.

How I am trying to approach the problem:

I think that solution folders are the solution to my problem. So I want to create a solution folder to which I will move all my scripts and content files.

However, my problem is that I have to move file by file to this folder since we cannot simply add an existing folder (I moved the scripts and content folders to the solution directory itself instead of being inside a specific project). And this is so tedious.

Here is what I tried to do:

  1. First I tried to locate where solution folders are created. However, I discovered that solution folders don't physically exist. I think they are just some kind of logical structuring to your solution. Whenever you add a file to a solution folder it is physically added to the solution directory.
  2. I thought that the solution file (.sln) may contain the configuration of solution folders and their content in some kind of xml format. However, this doesn't seem to be the case.
  3. I opened the SQL Server Compact Edition file (.sdf) of the solution thinking that may there is a DB table that describes the solution folders. However, this also doesn't seem to be the case.

So, finally, my question is:

Is there any easy way to add an existing folder as a solution folder in order to avoid adding files one by one (I have too many files).

Update:

Thanks for the answers and suggestions. I ended up taking a somewhat different approach. I used a pre-build command: XCOPY "$(SolutionDir)Files\*.*" "$(ProjectDir)" /E. I did this because the suggested solutions and my previous attempt was to add links/shortcuts to the external resource files. While this was fine and shortcuts were actually added to the project, when I tested the MVC project, IIS didn't manage to load the resource files since it looks for physical files (of course).

So my query now is: is there someway, whether a build event or and configuration in the csproj file, to "Include In Project" the newly "physically" copied files?

¿Fue útil?

Solución 5

My final solution to the problem is a combination of the solutions suggested answers and the approach I decided to take so that local IIS can see the files.

I added the following section to the csproj file:

    <Content Include="..\Files\**">
    <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
</Content>

I also added the following command as a pre-build event in my project:XCOPY /y "$(SolutionDir)Files\*.*" "$(ProjectDir)" /E

Otros consejos

As drew advised: editing the project file is one way. Another way is to edit the Solution file in a mechanism similar to that of the csproj file. In a solution file, a project with a specific GUID holds the details of a set of folders.

Write a small app that will read your folder and output a small text of the desired output. Generate new Guids until the file is created. Just paste this at the top of the .sln file. 2150E333-8FDC-42A3-9474-1A3956D46DE8 seems to be the GUID used for Solution Items.

This sample has a few subfolders with content in a nested structure. i.e.

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NewFolder1", "NewFolder1", "{4CDFCC66-45BD-4B6D-8758-FEF7E9F61C1C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SubFolder1", "SubFolder1", "{771BDBFB-5C01-4C51-A170-D88ECA8DE896}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SubFolder2", "SubFolder2", "{5D23AB90-5EF6-4611-A575-34F7B50BB1B6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NewFolder2", "NewFolder2", "{135E9F67-2DF8-4458-AC6D-FF82FC1B3BC6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SubSubFolder1", "SubSubFolder1", "{B41C7BAA-7E3D-405B-96AB-005120D12D26}"
ProjectSection(SolutionItems) = preProject
    NewFolder1\SubFolder1\SubSubFolder1\a.txt = NewFolder1\SubFolder1\SubSubFolder1\a.txt
EndProjectSection

EndProject

Solution folders (on the file system) are optional, and in Visual Studio they're typically used just to organise files within Visual Studio.

It's a manual approach to create a "physical" folder at the same level as the solution but this reduces complexity.

Below are the steps for moving a single script file:

On the File System

  1. Create a solution folder (typically at top-level directory)
  2. Copy script file from (one) project to solution folder

In Visual Studio

  1. Create a solution folder
  2. Right-click solution folder, select Add > Existing item...
  3. Select script file from the new solution folder on file system, then click Add
  4. Exclude/Remove script file from project1
  5. Right-click project1, select Add > Existing item...
  6. Select script file from solution folder on file system, select "Add as link" from the drop down on the Add button
  7. Repeat steps 6-8 for all other projects

By using "Add as link" the main thing we've done is to add a reference to the script in the solution folder as opposed to the actual file being copied to the project folder.

Note that you're not limited to working with one file at a time, it's just easier to track.

The fastest way I know of to find where you solution folder is located:

  1. Within Visual Studio, open the "Solution explorer if it's not already open View -> Other Windows -> Solution Explorer (CTRL + ALT + L)

  2. Within the solution exploerer, Right-click your project or solution

  3. Select Add -> New Item.

  4. The window that pops up will allow you to select a new file template, but what you're looking for is the "Location" field at the bottom. It should have the path to your solution folder. It's a safe bet that your solution folder is at this location or up a couple levels.

  5. Once you get the path to your solution folder you can click Cancel to exit the wizard.

As far as I can tell, visual studio doesn't provide this feature through their interface. The intention probably is that project files exist within the project folder rather than being separate. It generally makes working with projects easier (moving, sharing, etc).

That said, you don't have to have every real folder in a project location actually included in that project. So you could do something like the following folder structure:

+ /Projects (contains ProjectA.csproj and ProjectB.csproj)
    |
    +-- /ProjectA (folder with files that are just for "ProjectA"
    |
    +-- /ProjectB (folder with files just for "ProjectB")
    |
    +-- /Shared (Folder with your shared scripts/files)

With this structure you should be able to use the "Show All Files" button and include the shared folder in both projects. You will probably need to manually edit the csproj files to map to this new structure but it shouldn't be too difficult.

The only other option I can think of is to try to manually edit the csproj files such as this question shows.

Hope that all helps. Best of luck!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top