AmirSina Mashayekh
AmirSina Mashayekh

Reputation: 520

How to build a DLL with custom file extension in a C# project?

Is there any way to compile a C# DLL project (visual studio) into a file with a custom extension? for example plugin.cpx instead of plugin.dll (cpx = Custom Plugin eXtension!)

And can I use any extension I want?

Upvotes: 3

Views: 1306

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51825

I'm adding this as a new answer because it is just that. And it's a lot easier than my earlier offering, in many ways.

You can explicitly specify the target extension by manually editing the project's .csproj file.

In "Solution Explorer," right click on the project and select the "Unload project" command. Then, right-click again and select "Edit MyProject.csproj" (where "MyProject" would be the actual name, obviously).

To make the extension change apply to all configurations and/or platforms, add the following three lines, near the top of the file (just before the first of the other PropertyGroup entries):

  <PropertyGroup>
    <TargetExt>.cpx</TargetExt>
  </PropertyGroup>

Then save the file, close it, and "Reload" the project (again, via a right-click in the Solution Explorer).

To change the extension only for a specific configuration or platform, add the TargetExt line inside the PropertyGroup relevant to that configuration.

Upvotes: 4

Adrian Mole
Adrian Mole

Reputation: 51825

And can I use any extension I want?

Yes, you can, but it's a bit 'tricky'.

For C# projects, you will need to add a custom "Post Build Event" to rename the output file. For a DLL project (as yours is) you can add a post-build command like that shown below, in the "Build Events" tab of the "Properties" window:

enter image description here

Note: Be sure to use the $(TargetName) macro, not $(TargetFileName), as the latter will implicitly include the .dll extension. But you could use:

rename "$(TargetDir)$(TargetFileName)" "$(TargetName).cpx"

EDIT: Also, you will need to tell the builder to delete any old version of the .cpx file, at some stage. This could be done as a pre-build event, as follows:

IF EXIST "$(TargetDir)$(TargetName).cpx" del "$(TargetDir)$(TargetName).cpx"

Without this, the attempt to rename the output file will fail (as the new 'target' will already exist).

EDIT-2: As noted in the comment by the OP, without the " quotes, the pre- and post-build commands will fail if there are spaces in the file paths.

Upvotes: 1

Related Questions