正如标题所说。 我只知道如何在 C# 中执行此操作,但是当尝试使用 WPF 执行此操作时,我无法找到在程序启动时读取文件名的位置或内容。
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..");
}
}
最佳答案
找到解决方案。感谢您的帮助:)
如果其他人需要,我会发布我所做的..
在 App.xaml 中我添加了 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>
在 App.xaml.cs 中我添加了这些行:
public static String[] mArgs;
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length > 0)
{
mArgs = e.Args;
}
}
最后我不得不在 MainWindow 类中添加一些信息。
public MainWindow()
{
this.InitializeComponent();
String[] args = App.mArgs;
}
要获取所需的数据,请使用 System.IO.Path
注意 Using 语句。如果你只使用前。 Path.GetFileNameWithoutExtension
您将收到引用错误。获取数据时使用 System.IO.Path
。
关于c# - 将文件拖放到 .exe 文件顶部以获取文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3230790/