Programmatically work on project - c#

Is it possible to do these 3 things programmatically in Visual Studio:
Include new files to a project
Change 'Build Action' on a file in a project
I need to do this from a command prompt application but not sure if it is possible??

Yeah this is quite possible. You can create templates as .txt files with variables like $$$my_variable$$$ and just replace those with your inputted values. Can do whatever you want really. If you need an example, you could take a look at OrchardCMS, they have a command line generation tool that creates files/projects.
http://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.CodeGeneration/Services/CodeGenerationCommandInterpreter.cs
That is the file that contains the relevant code, there is a folder in that project with the templates. Hope that points you in the right direction!

Related

How do I link a source file between two proejcts in Visual Studio Code or dotnet command line?

I have some common code between two project solutions, and I would like to link a source file from one solution to another. Specifically, I would like to add/link a single .cs file from a different solution to my current one.
This seems to be a good answer. However, Visual Studio Code does not have the add as link or add existing item. It only have Add Folder to Workplace. Furthermore,dotnet add and dotnet sln add only takes .csproj file, which is the whole project.
So how do I achieve this?
We generally don't share sources files because it creates all sorts of problems.
What we do is put the code in a class library and share the project (or assembly) with anything that needs it

How to copy additional files on build?

I have a WebApi2 application and it references other projects within the solution. One of these includes some files that I want copied on build. These files have their Copy to Output Directory property set to true.
The files are correctly located in the bin folder of their assembly.
However:
After the build I need them to be copied to the App_Data folder of the web application
Included in the files that get published to IIS
Is it possible?
From my experience you are going to have to get your hands dirty with MSBuild. Its been ages since I've used it but you will need to use the Move Task
This is what Visual Studio uses in the background to build the projects. It may seem daunting but is actually pretty simple once you get used to it. There are tons of tutorials online.
Alternatively, and the most straightforward way, is that you include the files in the APP_DATA folder from within the solution.
EDIT: As mentioned by robor78 you will need to call the Move Task in the Post-Build event
Add build events
In Visual Studio -> Solution Explorer -> right click the project -> properties -> go to build events.
Use something like xcopy
Also if click on "Edit Post-Build" you will see a list of useful folders and file names which you can use e.g. $(OutDir)
You can use Xcopy from the Post build event. It is pretty straight forward but will not run if code not changed. So if you update configs etc. you must force the build I beleive.
See here for how to use xcopy.
http://commandwindows.com/xcopy.htm
There are wild cards and all kinds of useful features, but your situation is pretty simple. Just copy from $(ProjectDir)/bin or something like that to a path.

How can you add external info to C# program during build?

I want to add a small piece of information such as a software package version I have on my machine to my C# executable.
I want this info pulled on the fly during every build of my program. The info is just 1 line in a text file saved somewhere on C:\
The info needs to be accessible to my program, when I click a button within it, it should display that line of text it grabbed during the build.
Perhaps this could be accomplished by putting the info into a custom environment variable, which gets propagated during a build by adding a before build target. Not sure how to do that, if its even possible. Im using VS2008.
Please share your ideas!
VStudio has the option of performing pre-build steps. In our build, we have a batch file that executes tools that in turn generate a small file of C# which is then built by the build process. It's not very elegant, but has the benefit of simplicity and it works.
If you are the only person to be building this (or everyone building it has this text file in the same location), you could add your text file to your project as a linked item.
This way the file would be included with your project without creating a copy so any updates to it would be propagated to your project.
Right-click the project in solution explorer
Add->Existing Item...
Once you have navigated to your text file, click the down arrow icon at the right edge of the add button and select "Add as Link"
These steps are copied from this answer to a different question. That question was dealing with VS2010, but this should work in VS2008 just the same.

Is there a built-in way to access a project's csproj file data?

Or maybe access it's data via reflection somehow ?
Thanks
EDIT: I'd like to know if there's a way to do it without reading it as an XML.
Like a ms library that supports it, so it'll work for any kind of project and any kind of vs version (2005, 2008, 2010 ... ).
The *.csproj file is really just an XML file. You can open it just like any other XML file and treat it as such as well. Realize that any changes made to it, though, will require reloading the project.
Also, remember, there is no *.csproj file once the application is compiled/deployed.
Yes, you can open it in a text editor, or from Visual Studio
Right click on a project
Unload Project
Right click on the unloaded project
Edit project
Right clicking the project and selecting the properties option gives you a GUI to change some of the settings in the project file. Also, when you right click a file and change it's properties it causes a change in the project file. If you want to do anything very serious, like add custom build steps, you have to do it by hand in a text editor like notepad++. It's just an XML file. If you're familiar with MSBuild the proj file has many similarities to a build script for MSBuild.

Unable to access 'Build Action' property for .cs files

i got a problem with my site with my app_code files and a lot of question ive read people say you need to change the proprties of the .cs files to Build Action.
by right clicking the .cs files and press proprties.
but...
when i right clicking the files i cant see any option called proprties so i press F4 and it open a proprties window and i just click on the .cs file and still cant see any option i can change somthing to Build Action.
any idea why i cant see this options?
EDIT:
my problem is that when i get in the file proprties i have only 2 options...
thay are called:
1.Full Name
2.Full Path
EDIT2:
here what i see in visual studio 2010 when i try access the protrtie menu/window
http://img338.imageshack.us/img338/2359/blac.png
As Microsoft points out in the article "Web Application Projects versus Web Site Projects":
Web application projects use Visual Studio project files (.csproj or
.vbproj) to keep track of information about the project. Among other
tasks, this makes it possible to specify which files are included in
or excluded from the project, and therefore which files are compiled
during a build.
An answer to a similiar question at CodeProject's forums reveals a hint. Abstract:
[...] Looks like you are working on a web application that is actually a
Website as per Visual Studio. You would need to create a new Web Application
and probably copy over the source files there. [...]
http://www.codeproject.com/Questions/173637/Setting-Build-Action-for-Files-in-App_Data
Have you tried this:
You say you haven't got the option being suggested by other posters.
If this is the case, than it is quite possible that your Visual Studio settings are corrupt; this can give rise to all sorts of odd behaviour.
I would suggest you reset your settings, but please be aware you will lose any custom IDE settings that you've previously applied.
Try this:
In Visual Studio, go to Tools->Import and Export Settings
Choose "Reset All Settings" and click Next
Choose to save your current settings if you wish, or select "no" and then click next.
Choose the collection of settings( he IDE preset) you want, probably "Visual C# Development Settings"
Visual Studio will now revert all settings. Hopefully this will make the Build Action reappear.
[EDIT]
It might be worth trying safe mode too.
To do this, start up a "Visual Studio Command Prompt" from your start menu/programs list in Windows, and start Visual Studio with
devenv.exe /SafeMode
Does this make the options appear?
You can copy the file from Windows Explorer and paste it in the Solution Explorer. It will replace (or do nothing but incorporating it in the proj file) the file and recognize it as C#.
Normally you should see a Property named "Build Action" in the first line of the Property Window. This property should be set to "Compile".
Please select file and right click on it so that you will get following screen
Than click on the Properties you will get following screen
You can find build option in as a first option.

Categories

Resources