Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,

I have a program which having "PageMain.xaml" as startup. Now, I'm adding new window named "Settings.xaml" where it will be use to handle any configuration and store to "app.config" file. When I'm running a "MyApplication.exe" it will start with "PageMain.xaml".

Let's say before run the application, I want to open the Settings first to set the configuration and after complete save new configuration, I will run the EXE for main program. Is there any way to start the configuration window?

I have the script using batch file to open specific form. But it works on WinForms. How to do this using WPF? Below are the script for WinForms:-

PHP
@echo off
start MyWinForm.exe /config


The WinForm has recognition when program start, it will see if the send the args is like:-
C#
<pre lang="c#">private static void Main(string[] args){
if (args.Length.Equals(0)){
}else if (args.Length.Equals(1) && args[0].ToLower() == "/config"){
}
}

How to do it on VB.Net at Application_Startup?

What I have tried:

Private Sub Application_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles Me.Startup

If sender.Length.Equals(0) Then
LogEvents("Sender is sending " & sender.Length.Equals(0).ToString, EventLogEntryType.Information)
ElseIf sender.Length.Equals(1) AndAlso sender(0).ToLower() = "/config" Then
LogEvents("Sender is sending " & sender(0).ToLower().ToString, EventLogEntryType.Information)
Else
LogEvents("Sender is sending " & sender.Length.Equals(0).ToString, EventLogEntryType.Information)
End If

What I have tried goes exception with "Ex-Public member 'Length' on type 'App' not found"
Posted
Comments
Sergey Alexandrovich Kryukov 11-Feb-16 11:14am    
...
Sergey Alexandrovich Kryukov 11-Feb-16 11:14am    
You are not showing the fragment of code with this bug, which is Arg.Length (non-existing member). The code fragment you show is ugly but correct.

"args.Length.Equals(1)" tells the tail. Why not if "(args.Length == 1 && ...)"?

Also, you are showing both C# and VB.NET, why?

—SA
Luiey Ichigo 11-Feb-16 12:25pm    
Hi SA,

I'm showing both because C# is my previous staff in my office use to write code, while VB.NET is what I'm writing and try to do it in VB.NET and I don't know how to write it using VB.NET in WPF since it write in C# and WinForms. The code of Args if write on Program.cs
Sergey Alexandrovich Kryukov 11-Feb-16 13:56pm    
It's only important what you want to use now...
—SA

You would need to have a Main method in your application, just like in the c# code.
Main Procedure in Visual Basic[^]

Or you could get the commandline arguments using My.Application.CommandLineArgs

Good luck!
 
Share this answer
 
I have try Nijboer link and search a little more about it. And this is what I found:-
VB
Private Sub Application_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles Me.Startup
    Try
        If e.Args.Length <> 0 Then
            Me.StartupUri = New System.Uri("Settings.xaml", System.UriKind.Relative)
        ElseIf e.Args.Length = 0 Then
            If ComeOnce = False Then
                If udt_settings.bDebug = False Then
                    If CheckIfRunning("explorer") Then
                        obj_core.KillProcess("explorer")
                    End If
                    ChangeDesktopWallpaper()
                End If
                Me.StartupUri = New System.Uri("PageMain.xaml", System.UriKind.RelativeOrAbsolute)
                ComeOnce = True
            End If
        End If
    Catch ex As Exception
        LogEvents("Error occur at Application_Startup. Ex-" & Ex.ToString, EventLogEntryType.Information)
    End Try
End Sub

So I make 2 batch file. One named exec_program.bat and another one named exec_settings.bat.
exec_program.bat script:-
PHP
@echo off
start MyProgram.exe

exec_settings.bat script:-
PHP
@echo off
start MyProgram.exe /config

It just work well. Nice and thanks for help/guide.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900