.netpowershellmtpshell32wpd

shell32 copyhere not working neither in .Net nor powershell script


There is a powershell script to copy files between pc and android via MTP programmatically,see access-file-system-against-mtp-connection,and it works when it running under powershell console,then i have made a script as below:

function Usage()
{
    echo "Usage:mtpcmd cp srcfile dstfolder"
}
function main
{
    param($mainargs)
#   echo "args2:$mainargs"
    if(-not($mainargs.length -eq 2)) 
    {
        Usage
        return
    }
    $srcfile=$mainargs[0]
    $dstfolder=$mainargs[1]

    $phone = Get-ChildShellItem | where { $_.Path -like '*usb*' }
    echo "phonePath:$($phone.Path)"
    Copy-ShellItem -Path "$srcfile" -Destination "$($phone.Path)\内部存储$dstfolder"
}
#echo "args1:$args"
main -mainargs $args

This script works fine running on a powershell console,but when i run it under cmd like

powershell -Files mtpcp.ps1 c:\test \Android\test\

or

powershell ./mtpcp.ps1 c:\test \Android\test\

it just do nothing(no error shown).

and then i have implement the same functions on .Net using system.Shell32,the function CopyHere works fine with regulare path like c:\,but not work with mtp device path,just like powershell,it just do nothing on CopyHere function,neither a error message shown up.


Solution

  • well...just add an sleep or readkey after copyhere,then it works properly.

    Shell sh = new Shell();
    Folder folder = sh.NameSpace(AndroidMTPFiles.GetUsbFolderItem().Path + @"\内部存储\Android\test");
    //FolderItem fi = AndroidMTPFiles.GetRegularFileItemByPath(@"c:\test\src");
    folder.CopyHere(@"c:\test\src\",16);
    sh.Open(folder);
    System.Console.ReadKey();