4

I've developed VB.NET Winforms projects in the past that use the "Windows Application Framework" settings, which are found on the "Application" tab in the project properties:

enter image description here

One particularly useful feature of this is the ability to make a single-instance application with one click of a checkbox. This works very well. Here is the C# project properties for a Winforms project:

enter image description here

There's nothing about the application framework, neither on the "Application" tab nor elsewhere. All the references I've found to "single instance" applications talk about using a custom solution with a mutex. I can't find any information about why there is no equivalent application framework in C# projects. Can anyone shed some light on this?

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • There is no such thing in C# ... why it is in VB.NET ... prolly because there was such thing back in VB6 – Selvin Dec 10 '19 at 13:45
  • Why you wana use this anyway ... This is terrible ....You always can create VB.NET app with enabled options which you are interested in then compile it, then using tools like ILSpy see what code was generated. – Selvin Dec 10 '19 at 13:48
  • "Why you wana use this anyway ... This is terrible" you're going to explain that. What is terrible, and why? – rory.ap Dec 10 '19 at 13:51
  • The model that you have some global object, the complexity of underlaying type (Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase) – Selvin Dec 10 '19 at 14:03
  • A C# Application doesn't have that *commodity*. If you just need a SingleInstance app, you can use `Microsoft.VisualBasic.ApplicationServices` anyway. See this answer (one of the many, but a quite *clean* implementation): [Force Single Instance with Mutex handling restart application](https://stackoverflow.com/a/9457060/7444103) – Jimi Dec 10 '19 at 15:48
  • These features are available through using [WindowsFormsApplicationBase](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.applicationservices.windowsformsapplicationbase?WT.mc_id=DT-MVP-5003235&view=netframework-4.8). For example you can see an example of setting [shutdown style and splash screen](https://stackoverflow.com/a/59006931/3110834). – Reza Aghaei Dec 10 '19 at 17:25

1 Answers1

6

These properties are exclusively available in VB.Net project types. If you want to utilize these VB.Net properties in C# project then you need to add reference to Microsoft.VisualBasic assembly and create your custom App class inherited from Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase to access the protected members that appears in the project properties and run your windows form application via your custom app class instead of C# Application class methods.

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var app = new MyApp(new Form1());
        app.Run(args);
    }
}

internal class MyApp : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
    public MyApp(Form mainForm)
    {
        this.EnableVisualStyles = true;
        this.SaveMySettingsOnExit = true;
        this.IsSingleInstance = true;
        this.MainForm = mainForm;
    }
}

Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase provides predefined features to an winform application that are only available via VB.Net. There many predefined class libraries in Microsoft.VisualBasic assembly that are not available in C#.

vendettamit
  • 14,315
  • 2
  • 32
  • 54