Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag drop a file on top of .exe file to get fileinfo

Tags:

c#

wpf

fileinfo

As the title say. I know how to do this in C# only, but when trying to do this with WPF I can't find out where or what to add to read the filename when the program starts.

public static void Main(string[] args)
{
    if (Path.GetExtension(args[0]) == ".avi" || Path.GetExtension(args[0]) == ".mkv")
    {
        string pathOfFile = Path.GetFileNameWithoutExtension(args[0]);
        string fullPathOfFile = Path.GetFullPath(args[0]);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(pathOfFile, fullPathOfFile));
    }
    else
    {
        MessageBox.Show("This is not a supported file, please try again..");
    }
}
like image 328
Creator84 Avatar asked Jan 22 '23 18:01

Creator84


2 Answers

Found the solution. Thanks for your help :)

I'll post what I did if anyone else needs it ..

In App.xaml i added Startup="Application_Startup

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.App"
    StartupUri="MainWindow.xaml"
    Startup="Application_Startup">
    <Application.Resources>
    <!-- Resources scoped at the Application level should be defined here. -->
    </Application.Resources>
</Application>

And in App.xaml.cs i added these lines:

    public static String[] mArgs;

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.Args.Length > 0)
        {
            mArgs = e.Args;
        }
    }

Last i had to add some information in the MainWindow class.

public MainWindow()
{
    this.InitializeComponent();
    String[] args = App.mArgs;
}

To get the data you want, you use System.IO.Path

Watch out for the Using statement. if you only use Ex. Path.GetFileNameWithoutExtension you will get a reference error. Use System.IO.Path when getting your data.

like image 120
Creator84 Avatar answered Jan 24 '23 09:01

Creator84


You need to find some entry point (like OnLoad event for your main window) and then access the command line arguments like so:

string[] args = Environment.GetCommandLineArgs();
like image 26
Assaf Lavie Avatar answered Jan 24 '23 09:01

Assaf Lavie