Removing a FolderItem Object

I want to delete a file stored on a USB flash drive (this is actually an Android android SD card).

I ask the user to specify the application folder inside the SD card, and I hold an object Shell32.Folderpointing to it.

I can iterate between files ( FolderItemobjects), but how can I delete a file using Shell32classes?

Normal File.Delete(filePath)does not work because it FolderItem.Pathis a BSTR data type, so perhaps the key should convert one type to another, but I could not find a way to do this.

Any ideas?

EDIT 1:

FolderItem.Path data:

":: {20D04FE0-3AEA-1069-A2D8-08002B30309D} \\\\ USB # vid_04e8 &? Pid_6860 & ms_comp_mtp & GT-P5100 # 7 & 392be4e4 & 0 & 0000 # {6ac27878-a6fa-4155-ba85 -f983349d4 \ {10001 production siding, SECZ9519043CHOHB, 12530364416} \ {015C008D-013D-0145-D300-D300CB009500} \ {015C00EE-013D-0145-0201-37012C010901} \ {025901D2-029D-020F-DE0160010-0201 FE01601201001001001001

This is not a valid path for File.Delete. Any ideas on how to convert it?

EDIT 2:

A method that opens a view folder window, so the user can specify the application directory inside his Android device’s SD card, so I can iterate with folders and files and synchronize some data with the server. This is done due to problems between Win8 and Shell32:

public Shell32.Folder GetShell32NameSpace(Object folder)
{
    Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
    Object shell = Activator.CreateInstance(shellAppType);
    return (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
}

Using the method above to get the folder:

IntPtr windowHandle = new WindowInteropHelper(this).Handle;
Folder androidPath  = BrowseForFolder(windowHandle.ToInt32(), "App data folder", 0, 0);

"" , , :

byte[] data = new UTF8Encoding(true).GetBytes("sync_cable");

if (androidPath != null)
{
    foreach (FolderItem dir in androidPath.Items())
    {
        if (dir.IsFolder && dir.Name == "data")
        {
            Folder dataFolder = GetShell32NameSpace(dir.Path);
            if (dataFolder != null)
            {
                string tempPath = Path.GetTempPath();
                string path     = Path.Combine(tempPath, flagFileName);
                File.WriteAllBytes(path, data);

                Folder localPath = GetShell32NameSpace(tempPath);
                if (localPath != null)
                {
                    foreach (FolderItem file in localPath.Items())
                    {
                        if (file.Name.Contains(flagFileName))
                        {
                            dataFolder.CopyHere(file);
                            break;
                        }
                    }
                }
            }

            break;
        }
    }
}

, :

foreach (FolderItem file in dataFolder.Items())
{
    if (file.Name.Contains(flagFileName))
    {
        // THIS THROWS AN 'INVALID PATH' EXCEPTION
        // FileInfo fi = new FileInfo(file.Path);
        // fi.Delete();

        // THIS ALSO THROWS AN 'INVALID PATH' EXCEPTION            
        // File.Delete(file.Path);

        break;
    }
}

EDIT 1, file.Path , , :

":: {20D04FE0-3AEA-1069-A2D8-08002B30309D} \\\\USB # vid_04e8 &? Pid_6860 & ms_comp_mtp & GT-P5100 # 7 & 392be4e4 & 0 & 0000 # {6ac27878-a6fa-4155-ba85 -f98f491d4f33}\{10001 , SECZ9519043CHOHB, 12530364416}\{015C008D-013D-0145-D300-D300CB009500}\{015C00EE-013D-0145-0201-37012C010901}\{025901D2-029D-020F-DE01-FE010D02A601}"

3:

, P/Invoke .

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteFile(string lpFileName);

:

foreach (FolderItem file in dataFolder.Items())
{
    if (file.Name.Contains(flagFileName))
    {
        // ...
        bool deleted = DeleteFile(file.Path);
        // ...
    }
}

false, . , , , .

+4
2

guid ( ":: {20D04FE0-3AEA-1069-A2D8-08002B30309D}" ) MyComputer (. https://msdn.microsoft.com/en-us/library/windows/desktop/cc144096(v=vs.85).aspx).

, , shell API DeleteFile, ( FolderItem.IsFileSystem), Android MTP.

, FolderItem.InvokeVerb( "" ). , SendKeys (, Nirsoft Nircmd.exe). VBA:

Private Sub SampleDelete()

Const sFolder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_054c&pid_04cb#10fbd475671683#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,00000000000000000000000005671683,7513636864}\{00000007-0000-0000-0000-000000000000}\{000057F1-0000-0000-66A5-884D99020600}"
Const sFileName = "test.txt"

' requires reference to Shell32.dll
Dim oShell  As New Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oFile As Shell32.FolderItem

Set oFolder = oShell.Namespace(sFolder)
If Not oFolder Is Nothing Then
    Set oFile = oFolder.ParseName(sFileName)
    If Not oFile Is Nothing Then
        Debug.Print "File system? " & oFile.IsFileSystem
        ' dismiss the confirmation dialog
        SendKeys "~"
        ' Alternatively use an external function, like this freeware utility
        ' Shell "nircmd.exe sendkey enter press", vbHide
        oFile.InvokeVerb "Delete"
    Else
        Debug.Print "File not found"
    End If
Else
    Debug.Print "Folder not found"
End If

Set oShell = Nothing
Set oFolder = Nothing
Set oFile = Nothing

End Sub
0

Source: https://habr.com/ru/post/1533771/


All Articles