c# - 将文件拖放到 .exe 文件顶部以获取文件信息

标签 c# wpf fileinfo

正如标题所说。 我只知道如何在 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/

相关文章:

javascript - 转换 AI 和 EPS 文件以在 HTML Canvas 中使用

c# - 轴在点上绘制并防止在 DataVisualization.Charting 中进行选择

c# - 内部 StackPanel 不可见

wpf - 访问 WPF 中的 SQL Server 报表查看器控件

wpf - 如何在listview中绑定(bind)按钮

php - 文件信息 PHP 错误 - 找不到类 'finfo' 但模块在 php.ini 中

c# - ValidationRule 失败时属性绑定(bind)不更新

c# - 将表单控件的值传递给 ASP.NET 页面方法

C#:检索已安装屏幕保护程序的名称

C# - 添加、编辑和删除文件标签