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..");
}
}
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.
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();
Double-click the App.xaml file in Solution Explorer. You can add the Startup event. Its e.Args property gives you access to the command line arguments.
I know the topic is old but for someone searching for something similar it might be helpful. I was trying to add this feature to my program (starting the program by dropping a file on top of the EXE) and the solution was very simple and it came from here. My program was just messing with a couple of cells in Excel file. So it was pretty obvious that it should run and do that stuff just by dropping the excel file on it. So I added this into the Form constructor after the component initializing and it works flawlessly for me :
public Form1()
{
InitializeComponent();
string[] args = Environment.GetCommandLineArgs();
filePathTextBox.Text = (args.Length > 1 && (Path.GetExtension(args[1]) == ".xlsx" ||
Path.GetExtension(args[1]) == ".xls")) ? args[1] : "";
}
I noticed that the first argument in args
is the Path of my program, so I tested and apparently the second argument is the file path of the file I'm dropping on the exe, that's why I'm using args[1]
.
I would like to be corrected if I'm wrong but for now everything is working fine with my program.
You can use this for Mediaelement.
public MainWindow()
{
this.InitializeComponent();
doubleclickevent();
}
private void doubleclickevent()
{
string[] args = Environment.GetCommandLineArgs();
string[] arr = new string[]
{
".MP4",
".MKV",
".AVI"
};
foreach (string element in args)
{
foreach (string s in arr)
{
if (element.EndsWith(s))
{
MediaElement.Source = new Uri(@element, UriKind.RelativeOrAbsolute);
MediaElement.LoadedBehavior = MediaState.Play;
}
else { return; }
}
}
}
When you drag and drop files from Windows onto an EXE the EXE is launched and provided the file names as command line arguments.
public MainWindow()
{
string[] args = Environment.GetCommandLineArgs();
foreach (var s in args)
{
//do something with s (the file name)
}
}
精彩评论