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))
{
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, . , , , .