【问题标题】:User drags file onto my winforms executable file. Do I create some event listener?用户将文件拖到我的 winforms 可执行文件上。我要创建一些事件监听器吗?
【发布时间】:2019-12-15 23:29:26
【问题描述】:

我正在阅读来自此链接的 Stack Overflow 文章:Open File Associated Application,我遇到了另一个问题,即我的 Windows 窗体应用程序打开了,但我看不到内容。我应该在我的应用程序中做些什么来确保它能够处理从 Windows 资源管理器打开文件?用户会将文件拖到我的 winforms exe 上。

在 WinForms 应用程序中,您需要从 Environment.GetCommandLineArgs 方法中获取命令行参数。但是,调用此方法与控制台应用程序有一个重要区别:数组中的第一个元素包含正在执行的程序的文件名。如果文件名不可用,则第一个元素等于 String..::.Empty。其余元素包含在命令行中输入的任何附加标记。我找到了这段代码来存储参数,但我不知道在我的应用程序中实际实现什么事件。在 MSDN 线程上找到以下内容。

[STAThread]
static void Main()
{
  string[] args = Environment.GetCommandLineArgs();

  if (args.Length > 1)
  {
    string filePath = args[1]; //First arg is the running process

    if (File.Exists(filePath))
    {
      string name = Path.GetFileNameWithoutExtension(filePath);

      File.Copy(filePath, name + ".dat");

      //todo - delete input
    }
  }

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

【问题讨论】:

  • 您也可以将Main更改为:static void Main(string[] args)。在这种情况下,您的可执行文件的路径不存在。如果已传入参数,则第一个在 args[0] 中。在访问它之前,请务必检查args.Length。或者,如果您想使用Environment.GetCommandLineArgs(),您可以评估args[0] 以查看它是否包含可执行路径并(如果args.Length > 1)提取其他参数。
  • 顺便说一句,我将向name + ".dat" 添加一个完全限定的路径。
  • 我不确定我是否遵循。您有一个想要在打开时显示内容的 WinForms 应用程序?如果是这样,那么您将处理 Form.Load 并将代码以在处理程序中显示该内容。
  • 您还可以向 Form1 添加一个接受 string[] 参数的构造函数,修改 public MyAppMainForm() : this(null) { } 中的空构造函数。如果args[] 为空(或仅包含可执行路径),则运行Form1(),如果不为空,则运行Form(args)。因此,您可以在 Forms 的构造函数中测试 args 是否为空。如果不是,请提取参数并处理它们。您选择做什么取决于您要在何时/何地处理参数。

标签: c# winforms


【解决方案1】:

用户会将文件拖到我的 winforms exe 上。

您需要在目标控件中执行Drag-and-Drop 操作。假设它是Form1

首先,启用AllowDrop 属性:

Form1.AllowDrop = true;

处理DragEnter 事件以验证操作:

private void Form1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var file = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];

        if (File.Exists(file))
        {
            e.Effect = DragDropEffects.Move;
            return;
        }

        //If you need to allow certain type of files:
        //if (Path.GetExtension(file).Equals(".srcExt", StringComparison.OrdinalIgnoreCase))
        //{
        //    e.Effect = DragDropEffects.Move;
        //    return;
        //}
    }
    e.Effect = DragDropEffects.None;
}

然后,处理DragDrop 事件:

private void Form1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Effect == DragDropEffects.Move)
    {
        //according to your snippet:
        var srcFile = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
        //Another check is good, just in case:
        if (File.Exists(srcFile))
        {
            var destDir = @"The destination Directory";
            var destFile = string.Concat(
                Path.Combine(
                destDir,
                Path.GetFileNameWithoutExtension(srcFile)
                ),
                ".dat"
                );

            File.Move(srcFile, destFile);
        }
    }
}

至于Main 部分,我相信@Jimi 已经在他的cmets 中完美地涵盖了这一点,所以让我们从他那里偷一些来完成这篇文章:

[STAThread]
static void Main(string[] args)
{
    if(args.Length > 0 && File.Exists(args[0]))
    {
        var srcFile = args[0];
        var destDir = @"The destination Directory";
        var destFile = string.Concat(
            Path.Combine(
            destDir,
            Path.GetFileNameWithoutExtension(srcFile)
            ),
            ".dat"
            );

        File.Move(srcFile, destFile);
    }

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());            
}

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多