Here's the solution for WPF
Matthew showed me how to do this in windows forms application, and i researched a bit and found solution for wpf.
Here's step by step i did..
First I added a void Main function in App.Xaml.cs
public partial class App : Application
{
[STAThread]
public static void Main()
{
}
}
While compiling it showed an error Saying multiple entry points for the application. When double clicked, it navigated to the App.g.cs file where the actual entry point exists..
public partial class App : System.Windows.Application {
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent() {
#line 4 "..\..\App.xaml"
this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
#line default
#line hidden
}
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
FileOpen.App app = new FileOpen.App();
app.InitializeComponent();
app.Run();
}
}
.
Now i removed all lines here and copied the entry point to App.xaml.cs
And also removed the startupURI from App.xaml
<Application x:Class="FileOpen.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
</Application.Resources>
Now the App.g.cs
public partial class App : System.Windows.Application {
/// <summary>
/// Application Entry Point.
}
And the App.xaml.cs
public partial class App : Application
{
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main(string[] args)
{
MainWindow window = new MainWindow(args != null && args.Length > 0 ? args[0] : "");
window.ShowDialog();
}
}
And the MainWindow
public partial class MainWindow : Window
{
public MainWindow(string filePath)
{
InitializeComponent();
if (filePath != "")
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var sr = new StreamReader(fs)) txt.Text = sr.ReadToEnd();
}
}
using
blocks rather than manually closing theFileStream
andStreamReader
. It's not a big deal, but it's a good habit to get into. – Matthew Haugenusing
blocks. It's considered good practice. – mason