简体   繁体   中英

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 = C ustom P lugin e X tension!)

And can I use any extension I want?

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.

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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM