shell32 copyhere not working neither in .Net nor powershell script

南笙酒味 提交于 2019-12-11 07:34:17

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/40760114/shell32-copyhere-not-working-neither-in-net-nor-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!