0

I want my app to delete a bunch of files using the IFileOperation interface.

I found this code and I have added it as class into my project.

Then I use it like this:

public static void DeleteFiles(string data_path)
{
    string[] files = Directory.GetFiles(data_path, "*.*", SearchOption.AllDirectories);

    FileOperationG1 op = new FileOperationG1();
    op.From = files;
    op.Operation = FILEOP_CODES.FO_DELETE;
    op.Flags = FILEOP_FLAGS.FOF_NOCONFIRMATION;
    op.Flags = FILEOP_FLAGS.FOF_ALLOWUNDO;
    op.Execute();
}

And I get this error:

System.ArgumentNullException: Value cannot be null.
Parameter name: window
   at System.Windows.Interop.WindowInteropHelper..ctor(Window window)
   at ***.Classes.FileDir.FileOperationG1.Execute() in C:\Users\***\Desktop\Project Name\Classes\FileDir.cs:line 458

At line 458 is this part of code: WindowInteropHelper wih = new WindowInteropHelper(ParentWindow);

Any idea why is this happening?

Simos Sigma
  • 958
  • 7
  • 29
  • Put a breakpoint a that line, when check if the variable using for the argument of the method `WindowInterepHelper` is null From what I'm seeing from the error, it must be that – Camadas Sep 01 '22 at 13:16
  • 2
    Since you're using WinForms, you don't need `WindowInteropHelper`. Change the Property of Type `Window` to `IWin32Window` and pass `this` (which represents the instance of your Form) and use its `Handle` value directly (the Shell function call just requires a valid top-level Window Handle, specified in `[SHFILEOPSTRUCT].hwnd`) – Jimi Sep 01 '22 at 15:10
  • Seen is not working on WPF, so there is no need to use the class referenced by WindowInterepHelper .Implement the function, can you explain the type of project you use, so that the problem can be solved more clearly. – Lei Zhang-MSFT Sep 06 '22 at 08:37

1 Answers1

4

I think you will need to assign the "ParentWindow" property of your "op" object before running .Execute()

FileOperationG1 op = new FileOperationG1();

//  add this line VVVV
op.ParentWindow = ????? = "this object Window" // not real code
// fill in the ?????? ^^^^^^^

op.From = files;
op.Operation = FILEOP_CODES.FO_DELETE;
op.Flags = FILEOP_FLAGS.FOF_NOCONFIRMATION;
op.Flags = FILEOP_FLAGS.FOF_ALLOWUNDO;
op.Execute();
This Guy
  • 495
  • 4
  • 9
  • And what should I assign as `ParentWindow`? My main form or something? – Simos Sigma Sep 01 '22 at 13:31
  • The property is of type "Window", but I'm not sure the namespace. So, yes, try using your main form. – This Guy Sep 01 '22 at 13:32
  • public WindowInteropHelper (System.Windows.Window window); https://learn.microsoft.com/en-us/dotnet/api/system.windows.interop.windowinterophelper.-ctor?view=windowsdesktop-6.0 – This Guy Sep 01 '22 at 13:34
  • So it is a WPF window object... Because I am not working on WPF!!! – Simos Sigma Sep 01 '22 at 13:48
  • as the OP, you have the benefit and responsibility of asking the question. As a respondent, I can really only answer your question. You have asked a specific question. You want to use a class which has been written by someone else and you want to know how to use it. It might be beneficial to state what platform you are using and what function you would like to perform; then ask how best to perform the function. This allows a broad scope for answers, rather than a specific scope of answer ( using your already selected implementation ). – This Guy Sep 01 '22 at 14:02