ChatGPT解决这个技术问题 Extra ChatGPT

Why is "Set as Startup" option stored in the suo file and not the sln file?

It seems like this setting should be stored in the solution file so it's shared across all users and part of source code control. Since we don't check in the suo file, each user has to set this separately which seems strange.


O
Oliver

It is absolutely necessary that everyone can define their StartUp Project themselves, as Jon has already said. But to have a dedicated default one would be great, and as I can tell, it is possible!

If you don’t have a .suo file in your solution directory, Visual Studio picks the first project in your .sln file as the default startup project.

Close your Visual Studio and open the .sln file in your favorite text editor. Starting in line 4, you see all your projects encapsulated in Project – EndProject lines. Cut and paste the desired default startup project to the top position. Delete your .suo file. Open your solution in Visual Studio. Ta daa!


It seems to work only if not in a solution folder: I mean this trick works for root projects, from my experience with some solutions I have.
What if there are two default projects - how can I start them both by default?
@Oliver: Right-click Solution -> Set StartUp Projects... -> Multiple startup projects. One-click starts multiple.
@jdehaan I was able to change the startup project to one of the nested projects in VS2013 (I do not have any older versions of Visaul Studio): I have copied the containing folder as the first project in .sln file and the starup project as the second one.
@AdamPlocher I believe if no .suo file is around, VS more specifically picks the first "completely defined" csproj entry: if it lies in a subfolder, the subfolder must be declared as well, before any other project is "completely defined" like this. (Let me know if that does not make sense to you.)
P
Palec

In most cases, it does make sense to have a default on this.

It would be much better to accommodate a default startup project and store this in the .sln file, but which can be overridden by a developer in their .suo file. If the startup setting isn’t found in the .suo file, the default startup project in the .sln would be used.

Actually, this has been suggested on Visual Studio’s UserVoice.


The linked UserVoice is "closed for voting" but no comment as to why.
This is definitely the better stance compared to the accepted answer. I've not once worked within a team that considered it an advantage to set this on a per user basis. It just leads to extra setup on fresh clones, etc.
J
Jon Skeet

Why should it be a non-user-specific preference?

If I've got a solution with 10 files in, and one developer is primarily testing/using one of those tools, why should that affect what I start up?

I think MS made the right choice on this one. The project I want to start is far from necessarily the project that other developers want to start.


Frequently, you'll have a project which is very likely to be most people's desired startup project for debugging (eg. website), and you don't want a class library as a startup project. I don't see why MS couldn't provide a proper mechanism (not what seems like a hack, ie. putting the default one at the top in the .sln file) for setting a global default startup project, and then allowing an .suo to override it if desired.
There is, in a manner of speaking... Move the project you want to set as the default start up project to be the first project in the sln file. Delete your suo file, re-open the solution and woila, that first project should be the startup. There may be other factors that contribute to this, but I found it when I noticed that one particularly project kept defaulting when I checked out a clean project from source control.
This is a problem when using a build server because it would require checking the suo into version control to set the correct Startup project and checking the suo into version control is a bad idea.
I'm sorry, Jon, but I'm going to give you a -1 here. I am a believer that devs should have the ability to download code and then hit F5, expecting the most usual development setup to build and start. Of course, you should have the ability to tailor your startups for personal circumstances, but we should be able to set a default for most users, as stated above. Oliver's answer seems to go some way to do that, although it seems multiple start-up projects will remain impossible to check into source control, which is a shame.
Disagree that it should be a user setting. (Especially given how frequently the .suo file has to be deleted to correct for a VS load issue!) Personally, I'd like to the startup project to be set from the config/solution so that the startup project can change with the selected config. That makes more sense to me in a multi-project solution.
P
Palec

I wrote a little command line utility for Windows called slnStartupProject to set the Startup Project automatically:

slnStartupProject slnFilename projectName

I personally use it to set the startup project after generating the solution with cmake that always sets a dummy ALL_BUILD project as the first project in the solution.

The source is on GitHub. Forks and feedback are welcome.


Thanks for this! I was having exactly this problem with cmake and your utility works great!
You're welcome! Happy it helps as it's been bugging me for years before I decided to get that resolved for good. Don't really understand why people have philosophical fights about this while it's clearly useful in most cases.
You .. wait... what? -- you generate your .sln file manually? ... what sorcery is this?
c
ctrl-alt-delor

If you are using GIT, you can commit the default SUO file and then mark it as unchanged using

git update-index --assume-unchanged YourSolution.suo

It works also if you want to have more than one project in your default start group. The only disadvantage that I know about is that this command must be run by everyone who don't want to commit the SUO file.