How to insert a code snippet from a visual studio extension - c#

I am writing an extension for our group that creates a few folders and .cshtml files. This extension will also add a few dozen code snippets. I would really like to add some of the code snippets to the files when they are created so if a user changes their snippet it will also change in the generated code. Is there any way to access/insert code snippets from an extension?

The code snippet is usually added when developing/writing codes. Looks like you want to make this process automatically, like the newly created/inserted file has already included the related codes. And it seems you also want to make the snippet changeable.
I’m not sure if your requirements are more related to the project template or the more complex Visual Studio extension. I can share you a simple sample about how to insert a normal code snippet from a normal VS extension to the developed project/file, but it is manually. So perhaps it’s not what you want but I hope this could give you some references or a little help.
Sample
1). Install related VS SDK, and workload for creating Visual Studio Extension project. Create a VSIX project in VS.
2). Right-click the project in Solution Explorer and add one new folder(named it for example Snippets), after that add a subfolder named like My snippets(you can add other subfolders to classify), in this subfolder add some .snippet files.
3). Edit the .snippet files and add some related XML codes in it. Refer to this document: Snippet template.
4). Create a new file named XXXX.pkgdef and save it in project root folder, edit it and add related codes in it for registering. For example(for C#)
[$RootKey$\Languages\CodeExpansions\CSharp\Paths]
"MySnippets"="$PackageFolder$\Snippets\XXXXXXXX"
5). Right-click the source.extension.vsixmanaifest file > View Designer > Asserts > if there is a source, select it and click Edit > Choose related things:
6). (Optional) Edit the source.extension.vsixmanaifest file > Metadata to fill the related information that you want to set, such as Description, License Tags…
7). Hit F5 to debug and an Experimental instance of VS will launch, check the code snippet feature there.
Related documents threads/blog: Shipping Visual Studio snippets in an extension.

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

VSIX and item templates

So I'm having some trouble creating an item template and would like some help.
What I'm trying to do:
Create a template that adds 3 files. A Class.cs and two config files in the location "./Config/Acc/Config.xml" and "./Config/Prod/Config.xml".
I've managed to create the template through the wizard and editing the resulting files, but I would like an easy method of distributing the template on my teams TFS.
From some googling it seems that I should use A VSIX project to deply this easily. Problem is that I can't get it to compile. I have 2 projects: VSIXproject and ItemTemplateProject. I've set the assembly info on VSIX project to use the ItemTemplateProject and I've modified my Class.cs, but when I compile, visual studio doesn't know how to handle the Class.cs file.
What am I doing wrong? Is there a better way of including my ItemTemplate so that anyone who pulls the repo can use it?
The template project (a project that only contains Visual Studio project templates or item templates) is only used to be able to work with templates. Its output (say [myproject].dll) is not important (the project type may be C# but it's irrelevant) and you will only distribute the .vsix file in the end.
Files (.vstemplate files and any other files) in this project are like "static" files. They will also ultimately be included into the .vsix file output during build.
So, to ensure this files (.cs or other) are "static", you must make sure they have their action set to None (for example), not Compile.

where to find object code of my c# application

I am going for copyright my application which requires source code & object code.
So can anybody tell me that where are they located & how can i find these codes?
I am using Microsoft Visual Studio 2010
Your source code is the code you have written. This is stored wherever you've chosen to store you solution file. I believe the default location is:
C:\Users\{user}\Documents\Visual Studio 2010\Projects\{Project folder}
Your object code is the output from building your application. When you build in Visual Studio, the files are put into a \obj folder in the same location as your source code.
Here is a pretty good basic explanation of the two states of code: http://whatis.techtarget.com/definition/object-code
[Project Directory]\obj\ for the compiled, but not linked code and
[Project Directory]\bin\ for the compiled and linked code.
I imagine that you're looking for the latter in this case.
Please take a look to this What are the obj and bin folders (created by Visual Studio) used for?
Also if you click on a project and press the top button "Show all files" you will be able to see and access those files.

Programmatically work on project

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!

Syntax highlighting for non-project files in Visual Studio

I have a simple C# project which loads external C# files at startup to be used as scripts. Unfortunately when editing any of these 'non-project' files in Visual Studio I only get the most basic of syntax highlighting, since classes and types within the project are not known in the context of this external file.
Without adding the files to my project (defeating the purpose of them being external scripts), is there any way I can define an external interface or somehow otherwise convince Visual Studio (2008) to parse the code within these files in the context of the classes in the project?
A couple of clarifications (with thanks to the early answerers)
People should be able to edit these scripts without access to my source code
People shouldn't have to set up an entire Visual Studio project to edit one source file that's likely to contain less that 10 lines of actual code.
You will always need a reference to these classes. Maybe you can add these files as a link to the project or to a new project with a reference.
Visualstudio needs some informations to accomplish that.
I would think about the Bridge Pattern and you need to add the class body in the same file
http://en.wikipedia.org/wiki/Bridge_pattern
or using mock objects- you can easily use them to provide syntax highlighting without sharing your code (the same here - all in one file):
http://en.wikipedia.org/wiki/Mock_object
You can separate the script and the assiting classes if you would allow having a project file.

Categories

Resources