Simos Sigma
Simos Sigma

Reputation: 978

Issue using IFileOperation interface

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?

Upvotes: 0

Views: 365

Answers (1)

This Guy
This Guy

Reputation: 634

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();

Upvotes: 4

Related Questions