How can I programmatically retrieve a loaded project in Visual Studio 2008? - c#

I want to create an hour counter to monitor my development time on a project. I want the time to increase whenever a certain project is active in Visual Studio 2008.
Can I progammatically find out which *.csproj file is currently active in VS2008?

You can get the loaded solution via the DTE.Solution property, and the active document via DTE.ActiveDocument. There isn't really an "active project" because you never really activate projects, you only open solutions (which can contain multiple projects) and files (which happen to be in projects).
I suspect for a time-tracking requirement, getting the current solution would probably suffice. However, if you need to specifically distinguish between projects in the solution, you can do this using DTE.ActiveDocument.ProjectItem.ContainingProject.

Related

Reload all Projects after switching branches doesn't Reload Projects

I develop on a large web API that contains eight C# projects. Often, I am switching between branches within our repository to work on different tasks. I use Git Extensions as my tool of choice to work with Git, and whenever I checkout a different branch, Visual Studio will prompt me to reload the projects (if changes are detected). I normally select the "Reload All" button. After doing so, I keep getting errors being caused by what seems like left over changes from the branch I was previously on.
For example, if I am on branchA, which introduces a new file that contains a C# class, reference that class in someway in another already existing file, and then checkout branchB that doesn't contain these changes, Visual Studio will start to produce The type or namespace name 'name' does not exist in the namespace (are you missing an assembly reference?). This also seems to happen the other way around, where if I switch to a branch that introduces new changes not on my previous branch, these errors crop up.
It seems as if the .csproj files for the projects aren't being detected as updated after selecting the "Reload All" button. Evidence of this is that Solution Explorer still shows these files, but with red "X" symbols on them, denoting that the projects expects the file to be present, but it currently is not present on disk. I've made sure that the .csproj files do in fact get updated after changing branches. The only solutions that work for me currently is if I right click on the project(s) and unload then reload them, or if I close the solution/Visual Studio and reopen - both of which are tedious.
I am confident that this was not the behavior previously witnessed in prior versions of Visual Studio. I use to be able to freely switch between branches and not have this happen in the past.
Could something be out of sync? How can I resolve this?
This is with Visual Studio 2019 Enterprise (version 16.10.3).
Faced the same problem after updating Visual
In 16.8 version Microsoft added new Git experience that broke the "reload all" feature.
You have two ways to work with this:
Go to Tools → Options → Environment → Preview Features. Turn off the option "New Git user experience."
Use the Visual studio git panel to checkout (Git → Manage branch).
If you reload projects one-by-one instead of clicking Reload All, it should update them correctly. Not a solution, but a slightly-better workaround until it's fixed.
Unchecking "New Git user experience" doesn't work for me, hence this alternative approach.

One file keeps opening in visual studio IDE

I'm using Visual Studio 2010
In one solution I have A.XAML file and it keeps opening every time I start solution. It also opens at some random time - I have no exact explanation.
It's very annoying. Why would VS open specific file by itself?
Does it open even when you create a new project? How about locating the file and renaming it does it help the problem? Also, have you tried to backup and remove the content of C:\Users\<your_username>\AppData\Local\Microsoft\VisualStudio\?
Update 1 based on comments:
So when you renamed all your A.XAML to B.XAML, do you get the A.XAML appearing or do you get a B.XAML file? Do you have many instances of the A.XAML file on your computer? If there are many, rename them differently so that you can subsequently identify the guilty one. Have you tried to 'Repair' VisualStudio through 'Add Remove Program' in Control Panel? Can you locate the A.XAML string in Regedit.exe? I think it could be useful if you would create a dummy project and ask a colleague or, friend to open it on their computer? (It would be helpful to get have a sample of that dummy project in order to troubleshoot whether it is the Project or, the IDE or, the OS which is the guilty party)
Update 2 based on further comments:
Recreating the .SUO file of the project fixed the problem. For example, renaming the .SUO file to .BAK and re-opening the project in Visual Studio.
What worked for me was running Visual Studio installer and choosing the repair option.
I had a whole solution that always opened a few seconds after I open any other solution. I didn't want to delete the solution so I thought I should try repairing Visual Studio (using VS2019). Haven't had any issue since.
Seems like an 'intense' option but it was what worked.

How to attach a datafile to a windows app installation

I'm writing a windows application in c#,FW3.5 & Visual Studio 2008. I need to attach one or more datafiles to the installation.
The requirement is that when the end user installs the application, a folder will be created and the datafile will be put in this folder.
Is there a way to do this by amending the publish settings in Visual Studio or perhaps even do it programmatically?
Thanks for any advice.
Just thought I'd add that it's for a software package shipped to users on a 3,6 or 12 month licence. The company employee(non-programmer) needs to be able to specify the duration of the licence and which datafiles to ship with it before sending. I had tried unsuccessfully to persuade him to do away with the datafiles and just link directly to a database and now i'm stuck.
Thanks again.
You can create a setup project .Please refer to http://www.c-sharpcorner.com/UploadFile/vishnuprasad2005/SetupProjects12022005022406AM/SetupProjects.aspx
And then choose the application folder, create your directory and add your files to that folder.
Take a look at creating a Visual Studio Installer project. You can use the wizard to create one based on your current C# project. In the VSI project, you can adjust all sorts of things, including creating folders and publishing files to said folders.
just right-click in your project-view your main-project, select add folder, name that folder like you want, right-click it, select add existing element and choose the file you want to add.
Remember to deliver it with your release!
P.S.: This is for VS2008 and 2010! The labels might be namned other, cause I'm using the german language here!

TFS Check-in Unavailable

I have a C# project that is booked into TFS. However, for some reason one of the forms within the project has not booked in. I can’t work out how to force this to book in, as right-clicking on the source file doesn’t give me the option to check-in.
All the other files in the project are booked in, and I can see them using the Source Control Explorer, however, this file isn’t in there. Can anyone tell me how this could have happened, and how to force this file to check-in?
Right click in the source control explorer directory where the files live and select the Add Items to Folder Option.
This typically happens if you create the files outside of visual studio. That being said, TFS is flaky at best.

Simplifying setup and deployment in c#

I have made an application, which keeps getting updated frequently. So every time a change occurs, i've to include it's fresh builds to the setup and deployment program again and again. Is there any way to simplify the procedure? The files to be added are static in number and exist in a folder. I've heard we can write installer classes in c#, does my requirement has any thing to do with it?
I think ClickOnce doesn't suit my requirement because, for the first time i want it to run like a setup package, since it has some packages and some settings needed to be implemented on the user's machine at the time of install. Can click once help me with that? Also i want to run my application as an administrator and it references to many external dll files. So will it help my purpose?
I finally did it using clickonce deployment. I used content files to mark all the files i wanted to copy to the target computer and used clickonce deployment. Then i modified the way my program starts, so that i can lauch the installer script i wanted to run only when the app runs for the first time. Further i hosted it on IIS and had to change lot of MIME types and add new ones for the download to work over internet
Look into something called "ClickOnce" deployment. It automates a lot of what you're talking about.
EDIT: You can add custom installer actions to a ClickOnce project just like any other, to set up additional components and whatnot. As for permissions, ClickOnce will let you run as administrator if you so choose, but that sort of thing isn't recommended, and it might whine about it.
You can use ClickOnce (http://msdn.microsoft.com/en-us/library/t71a733d(VS.80).aspx) which simplify the deployment process.
Maybe you can also automate the build process using NANT (http://nant.sourceforge.net/).
HTH
Yes, you can do that.
I assume you want the client to update itself when ever there is a new version.
This needs a few changes in the client code. Essentially how it works is check for availablilty of new version at a predefined location. Update you new versions to this location. On the client side, show a message to the user if he/she wants to upgrade to the new version.
You can find a link to sample project out here and here.
You can add a Setup project in your solution inside Visual Studio and then add your other project(s) outputs, or static files to the Setup project as references. The Setup project will then detect your dependencies automatically and each time you do a Rebuild All (or you rebuild/build your Setup project) it will automatically include all the necessary files.
What type of project is it? In many cases, ClickOnce can do the job for you, at nominal effort.
Beyond that - you can usually hook your installer build into your build process; some tools will do this for you.
Installer classes run at the client - so I don't think they relate to your build process...
I would flag the files as Content in their respective properties and then in the deployment project right click the project, go to File System and then right click the folder, click Add and select Content Files from the dialog box. This should copy the newest files over every time you build the deployment project.

Categories