Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Program without a Window

I'm wondering whether it is possible to 'turn off' my main Window from loading automatically when my program starts with a command-line argument (i.e. when a file name is passed). The problem I have is that my program loads when a file associated with it is clicked, but does so by opening another main window and using that. The problem I have is that the program still launches the MainWindow afterwards, thus opening two Windows, one with the file contents and one that is empty.

How do I prevent the blank Window? As I see it, I either stop it from opening the main Window, close the main Window or make the program pass the file to the main Window. My problem is that I don't know which of these would be the best or how to to do it.

This is the code:

    protected override void OnStartup(StartupEventArgs e)
    {
        if (e.Args != null && e.Args.Count() > 0)
        {
            this.Properties["ArbitraryArgName"] = e.Args[0];
        }
        base.OnStartup(e);

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {

            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.Show();
            mw.readVcard(fname);
            Application.Current.Windows.
        }
    }

EDIT:

My solution is at the bottom.

like image 908
user646265 Avatar asked Aug 26 '11 21:08

user646265


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

I believe you can add a separate class with its own Main method and set that to be the entry point of your executable. Then you can parse the method arguments there and either bring up the main window or not.

(I'm assuming this is a WPF app - it's simpler in a WinForms app as you can modify the original Main method directly.)

like image 60
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet


I assume you use WPF? You'll want to replace the entry point (Main) that WPF supplies for you. Then, you can start WPF or not depending on the command-line arguments. See this question for more info:

Replacing the WPF entry point

like image 40
Qwertie Avatar answered Sep 27 '22 22:09

Qwertie