Reputation: 129
I want to run an mp3 file in myplayer.exe (I have coded and developed in c#) . But I am getting this error - abc.mp3 is not a valid Win32 application.
I used this code to get the filepath - ...
if
((AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null)
&&
(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length>0))
{
string fname = "No filename given";
try
{
fname = AppDomain.CurrentDomain.SetupInformation.
ActivationArguments.ActivationData[0];
Uri uri = new Uri(fname);
fname = uri.LocalPath;
this.Properties["ArbitraryArgName"] = fname;
}
catch (Exception ex)
{ }
base.OnStartup(e);
}
The above code in the app.xaml.cs
and In the mainWindow.xaml.cs , this is the code I have used!
public CubeWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainContainer_Loaded);
}
void MainContainer_Loaded(object sender, RoutedEventArgs e)
{
if(System.Windows.Application.Current.Properties["ArbitraryArgName"] != null)
{
string fname=System.Windows.Application.Current.
Properties["ArbitraryArgName"].ToString();
me.Source = new Uri(fname, UriKind.RelativeOrAbsolute);
me.Play(); //me is the mediaelement
}
}
Please let me know to correct this.. and the cause of this error! Thanks in advanced! :)
Upvotes: 0
Views: 523
Reputation: 690
From the error description you had given it looks like a problem related to command line parameters. You have to read the command line parameters for your application. In WPF you can process your Application_Startup event to read the command line arguments. You can follow this to understand use of command line arguments in wpf. This will help you get into the details of playing MP3 files in C#..
Hope this will help.
Upvotes: 0
Reputation: 63
Judging from the error you're getting, it seems as though you're trying to execute an MP3. That won't work.
When you try to open a file - say, a txt file - by double clicking, Windows checks the registry for the default application for that file - which in most setups, would be Notepad. It then sends this command:
"<System32 Directory>\Notepad.exe" <filename>
or
"C:\Windows\System32\Notepad.exe" "C:\Users\user\Desktop\test1.txt"
So the first command line argument is the filename.
Long story short: If your program is started through trying to open an MP3 in Windows Explorer, you need to get and save the command line arguments for reference somewhere in your project.
To do this, you can use Environment.GetCommandLineArgs()
or pass them from static void main
in Program.cs
to your first form's constructor.
Upvotes: 1