I want to Build an app that let me to pick a file in Android device a transfer it to my pc. Is there a way of doing this with c#? Thanks
I want to Build an app that let me to pick a file in Android device a transfer it to my pc. Is there a way of doing this with c#? Thanks
Hi Costorix31, Thanks for your code. If i have t change the destination folder from documents to any other location. How to achive that.
thanks
A test adapted from the code I had posted (I removed the Button (in comments) and simplified a bit..)
I set the WPD folder as selected folder on opening, and the selected files are copied to the Documents folder =>
public partial class Form1 : Form
{
public enum HRESULT : uint
{
S_OK = 0,
S_FALSE = 1,
E_NOINTERFACE = 0x80004002,
E_NOTIMPL = 0x80004001,
E_FAIL = 0x80004005,
E_INVALIDARG = 0x80070057,
E_CANCELLED = 0x80070000 + ERROR_CANCELLED,
E_UNEXPECTED = 0x8000FFFF
}
public const uint ERROR_CANCELLED = 1223;
[ComImport]
[System.Runtime.InteropServices.Guid("947aab5f-0a5c-4c13-b4d6-4bf7836fc9f8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileOperation
{
//uint Advise(IFileOperationProgressSink pfops);
HRESULT Advise(IntPtr pfops, ref uint pdwCookie);
HRESULT Unadvise(uint dwCookie);
HRESULT SetOperationFlags(FILEOP_FLAGS dwOperationFlags);
HRESULT SetProgressMessage([MarshalAs(UnmanagedType.LPWStr)] string pszMessage);
HRESULT SetProgressDialog([MarshalAs(UnmanagedType.Interface)] object popd);
HRESULT SetProperties([MarshalAs(UnmanagedType.Interface)] object pproparray);
HRESULT SetOwnerWindow(uint hwndParent);
HRESULT ApplyPropertiesToItem(FileDialog.IShellItem psiItem);
HRESULT ApplyPropertiesToItems([MarshalAs(UnmanagedType.Interface)] object punkItems);
HRESULT RenameItem(FileDialog.IShellItem psiItem, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName,
//IFileOperationProgressSink pfopsItem);
IntPtr pfopsItem);
HRESULT RenameItems([MarshalAs(UnmanagedType.Interface)] object pUnkItems, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName);
HRESULT MoveItem(FileDialog.IShellItem psiItem, FileDialog.IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName,
//IFileOperationProgressSink pfopsItem);
IntPtr pfopsItem);
HRESULT MoveItems([MarshalAs(UnmanagedType.Interface)] object punkItems, FileDialog.IShellItem psiDestinationFolder);
HRESULT CopyItem(FileDialog.IShellItem psiItem, FileDialog.IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszCopyName,
//IFileOperationProgressSink pfopsItem);
IntPtr pfopsItem);
HRESULT CopyItems([MarshalAs(UnmanagedType.Interface)] object punkItems, FileDialog.IShellItem psiDestinationFolder);
HRESULT DeleteItem(FileDialog.IShellItem psiItem,
//IFileOperationProgressSink pfopsItem);
IntPtr pfopsItem);
HRESULT DeleteItems([MarshalAs(UnmanagedType.Interface)] object punkItems);
HRESULT NewItem(FileDialog.IShellItem psiDestinationFolder, System.IO.FileAttributes dwFileAttributes, [MarshalAs(UnmanagedType.LPWStr)] string pszName,
[MarshalAs(UnmanagedType.LPWStr)] string pszTemplateName,
//IFileOperationProgressSink pfopsItem);
IntPtr pfopsItem);
HRESULT PerformOperations();
[return: MarshalAs(UnmanagedType.Bool)]
bool GetAnyOperationsAborted();
}
[Flags]
public enum FILEOP_FLAGS : uint
{
FOF_MULTIDESTFILES = 0x0001,
FOF_CONFIRMMOUSE = 0x0002,
FOF_SILENT = 0x0004, // don't create progress/report
FOF_RENAMEONCOLLISION = 0x0008,
FOF_NOCONFIRMATION = 0x0010, // Don't prompt the user.
FOF_WANTMAPPINGHANDLE = 0x0020, // Fill in SHFILEOPSTRUCT.hNameMappings
// Must be freed using SHFreeNameMappings
FOF_ALLOWUNDO = 0x0040,
FOF_FILESONLY = 0x0080, // on *.*, do only files
FOF_SIMPLEPROGRESS = 0x0100, // means don't show names of files
FOF_NOCONFIRMMKDIR = 0x0200, // don't confirm making any needed dirs
FOF_NOERRORUI = 0x0400, // don't put up error UI
FOF_NOCOPYSECURITYATTRIBS = 0x0800, // dont copy NT file Security Attributes
FOF_NORECURSION = 0x1000, // don't recurse into directories.
FOF_NO_CONNECTED_ELEMENTS = 0x2000, // don't operate on connected file elements.
FOF_WANTNUKEWARNING = 0x4000, // during delete operation, warn if nuking instead of recycling (partially overrides FOF_NOCONFIRMATION)
FOF_NORECURSEREPARSE = 0x8000, // treat reparse points as objects, not containers
FOFX_NOSKIPJUNCTIONS = 0x00010000, // Don't avoid binding to junctions (like Task folder, Recycle-Bin)
FOFX_PREFERHARDLINK = 0x00020000, // Create hard link if possible
FOFX_SHOWELEVATIONPROMPT = 0x00040000, // Show elevation prompts when error UI is disabled (use with FOF_NOERRORUI)
FOFX_EARLYFAILURE = 0x00100000, // Fail operation as soon as a single error occurs rather than trying to process other items (applies only when using FOF_NOERRORUI)
FOFX_PRESERVEFILEEXTENSIONS = 0x00200000, // Rename collisions preserve file extns (use with FOF_RENAMEONCOLLISION)
FOFX_KEEPNEWERFILE = 0x00400000, // Keep newer file on naming conflicts
FOFX_NOCOPYHOOKS = 0x00800000, // Don't use copy hooks
FOFX_NOMINIMIZEBOX = 0x01000000, // Don't allow minimizing the progress dialog
FOFX_MOVEACLSACROSSVOLUMES = 0x02000000, // Copy security information when performing a cross-volume move operation
FOFX_DONTDISPLAYSOURCEPATH = 0x04000000, // Don't display the path of source file in progress dialog
FOFX_DONTDISPLAYDESTPATH = 0x08000000, // Don't display the path of destination file in progress dialog
}
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern HRESULT SHCreateItemFromParsingName(string pszPath, IntPtr pbc, ref Guid riid, ref IntPtr ppv);
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern HRESULT SHGetKnownFolderItem(ref Guid rfid, KNOWN_FOLDER_FLAG flags, IntPtr hToken, ref Guid riid, ref IntPtr ppv);
public enum KNOWN_FOLDER_FLAG: uint
{
KF_FLAG_DEFAULT = 0x00000000,
KF_FLAG_FORCE_APP_DATA_REDIRECTION = 0x00080000,
KF_FLAG_RETURN_FILTER_REDIRECTION_TARGET = 0x00040000,
KF_FLAG_FORCE_PACKAGE_REDIRECTION = 0x00020000,
KF_FLAG_NO_PACKAGE_REDIRECTION = 0x00010000,
KF_FLAG_FORCE_APPCONTAINER_REDIRECTION = 0x00020000,
KF_FLAG_NO_APPCONTAINER_REDIRECTION = 0x00010000,
KF_FLAG_CREATE = 0x00008000,
KF_FLAG_DONT_VERIFY = 0x00004000,
KF_FLAG_DONT_UNEXPAND = 0x00002000,
KF_FLAG_NO_ALIAS = 0x00001000,
KF_FLAG_INIT = 0x00000800,
KF_FLAG_DEFAULT_PATH = 0x00000400,
KF_FLAG_NOT_PARENT_RELATIVE = 0x00000200,
KF_FLAG_SIMPLE_IDLIST = 0x00000100,
KF_FLAG_ALIAS_ONLY = 0x80000000,
}
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private void Form1_Load(object sender, EventArgs e)
{
button1 = new Button();
listBox1 = new ListBox();
button1.Location = new System.Drawing.Point(190, 24);
button1.Name = "Button1";
button1.Size = new System.Drawing.Size(75, 23);
button1.TabIndex = 0;
button1.Text = "Choose Files";
button1.UseVisualStyleBackColor = true;
button1.Click += new System.EventHandler(button1_Click);
listBox1.FormattingEnabled = true;
listBox1.Location = new System.Drawing.Point(10, 77);
listBox1.Name = "ListBox1";
listBox1.Size = new System.Drawing.Size(460, 174);
listBox1.TabIndex = 1;
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.ClientSize = new System.Drawing.Size(480, 261);
this.Text = "Test IFileDialog and copy items to Documents folder";
CenterToScreen();
}
private void button1_Click(object sender, EventArgs e)
{
FileDialog fileDialog;
//string sFolder = "E:\\temp";
// WPD
string sFolder = "shell:::{35786D3C-B075-49b9-88DD-029876E11C01}";
FileDialog.FOS nOptions = (FileDialog.FOS.FOS_FORCESHOWHIDDEN | FileDialog.FOS.FOS_ALLOWMULTISELECT | FileDialog.FOS.FOS_NODEREFERENCELINKS);
//FileDialog.FOS nOptions = FileDialog.FOS.FOS_PICKFOLDERS;
string sFile = null;
object sFileNames = new System.Collections.ObjectModel.Collection<string>();
FileDialog.IShellItem si = null;
FileDialog.IShellItemArray sia = null;
fileDialog = new FileDialog(nOptions, "Title", sFolder);
if ((fileDialog.ShowDialog(this.Handle) == DialogResult.OK))
{
listBox1.Items.Clear();
if (fileDialog.FileName != null)
{
sFile = fileDialog.FileName;
si = fileDialog.ShellItem;
listBox1.Items.Add(sFile);
// Test copy 1 file to Documents folder
IFileOperation pfo;
Guid CLSID_FileOperation = new Guid("3ad05575-8857-4850-9277-11b85bdb8e09");
Type fileOperationType = Type.GetTypeFromCLSID(CLSID_FileOperation);
pfo = (IFileOperation)Activator.CreateInstance(fileOperationType);
Guid GUID_IShellItem = typeof(FileDialog.IShellItem).GUID;
IntPtr psi = IntPtr.Zero;
Guid FOLDERID_Documents = new Guid("FDD39AD0-238F-46AF-ADB4-6C85480369C7");
SHGetKnownFolderItem(ref FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, IntPtr.Zero, ref GUID_IShellItem, ref psi);
if (psi != IntPtr.Zero)
{
FileDialog.IShellItem pShellItemDest = (FileDialog.IShellItem)Marshal.GetObjectForIUnknown(psi);
HRESULT hr = pfo.CopyItem(si, pShellItemDest, null, IntPtr.Zero);
hr = pfo.PerformOperations();
Marshal.ReleaseComObject(pShellItemDest);
}
Marshal.ReleaseComObject(pfo);
Marshal.ReleaseComObject(si);
}
else if (fileDialog.FileNames != null)
{
sia = fileDialog.ShellItemArray;
sFileNames = fileDialog.FileNames;
int nCount = ((System.Collections.ObjectModel.Collection<string>)sFileNames).Count;
for (int n = 0; n < nCount; n++)
{
string sFileName = ((System.Collections.ObjectModel.Collection<string>)sFileNames).ElementAt(n);
listBox1.Items.Add(sFileName);
}
// Test copy n files to Documents folder
IFileOperation pfo;
Guid CLSID_FileOperation = new Guid("3ad05575-8857-4850-9277-11b85bdb8e09");
Type fileOperationType = Type.GetTypeFromCLSID(CLSID_FileOperation);
pfo = (IFileOperation)Activator.CreateInstance(fileOperationType);
Guid GUID_IShellItem = typeof(FileDialog.IShellItem).GUID;
IntPtr psi = IntPtr.Zero;
Guid FOLDERID_Documents = new Guid("FDD39AD0-238F-46AF-ADB4-6C85480369C7");
SHGetKnownFolderItem(ref FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, IntPtr.Zero, ref GUID_IShellItem, ref psi);
if (psi != IntPtr.Zero)
{
FileDialog.IShellItem pShellItemDest = (FileDialog.IShellItem)Marshal.GetObjectForIUnknown(psi);
HRESULT hr = pfo.CopyItems(sia, pShellItemDest);
hr = pfo.PerformOperations();
Marshal.ReleaseComObject(pShellItemDest);
}
Marshal.ReleaseComObject(pfo);
Marshal.ReleaseComObject(sia);
}
}
}
public class FileDialog
{
public FileDialog(FOS nOptions = 0, string sTitle = null, string sFolder = null)
{
Options = nOptions;
Title = sTitle;
Folder = sFolder;
FileNames = new System.Collections.ObjectModel.Collection<string>();
}
public FOS Options { get; set; }
public string Title { get; set; }
public string Folder { get; set; }
public string FileName { get; set; }
public System.Collections.ObjectModel.Collection<string> FileNames { get; set; }
public IShellItem ShellItem { get; set; }
public IShellItemArray ShellItemArray { get; set; }
private DialogEventSink eventSink;
public DialogResult ShowDialog(IntPtr hwndOwner)
{
HRESULT hr = HRESULT.E_FAIL;
IFileOpenDialog fod = (IFileOpenDialog)new FileOpenDialog();
try
{
FOS nOptions = 0;
hr = fod.GetOptions(out nOptions);
nOptions = nOptions | Options;
hr = fod.SetOptions(nOptions);
if (!string.IsNullOrEmpty(Title))
hr = fod.SetTitle(Title);
if (!string.IsNullOrEmpty(Folder))
{
Guid GUID_IShellItem = typeof(IShellItem).GUID;
IntPtr psi = IntPtr.Zero;
hr = SHCreateItemFromParsingName(Folder, IntPtr.Zero, ref GUID_IShellItem, ref psi);
if ((hr == HRESULT.S_OK))
{
IShellItem si = (IShellItem)Marshal.GetObjectForIUnknown(psi);
hr = fod.SetFolder(si);
Marshal.ReleaseComObject(si);
}
}
uint nCookie;
eventSink = new DialogEventSink(this);
hr = fod.Advise(eventSink, out nCookie);
eventSink.Cookie = nCookie;
IFileDialogCustomize pfdc;
pfdc = (IFileDialogCustomize)fod;
//hr = pfdc.AddPushButton(601, " Return selected items ");
hr = fod.Show(hwndOwner);
if ((hr == HRESULT.S_OK))
{
IShellItem pShellItemResult = null;
System.Text.StringBuilder sbResult = new System.Text.StringBuilder(MAX_PATH);
hr = fod.GetResult(out pShellItemResult);
if ((hr == HRESULT.S_OK))
{
ShellItem = pShellItemResult;
hr = pShellItemResult.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, ref sbResult);
if ((hr == HRESULT.S_OK))
{
FileName = sbResult.ToString();
}
else
{
hr = pShellItemResult.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, ref sbResult);
if ((hr == HRESULT.S_OK))
{
FileName = sbResult.ToString();
}
}
//Marshal.ReleaseComObject(pShellItemResult);
}
else if (hr == HRESULT.E_UNEXPECTED)
{
IShellItemArray pShellItemArrayResult = null;
hr = fod.GetResults(out pShellItemArrayResult);
if ((hr == HRESULT.S_OK))
{
ShellItemArray = pShellItemArrayResult;
int nCount = 0;
hr = pShellItemArrayResult.GetCount(ref nCount);
for (int i = 0; i <= nCount - 1; i++)
{
IShellItem si = null;
hr = pShellItemArrayResult.GetItemAt(i, ref si);
if (hr == HRESULT.S_OK)
{
hr = si.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, ref sbResult);
if ((hr == HRESULT.S_OK))
{
FileNames.Add(sbResult.ToString());
}
else
{
hr = si.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, ref sbResult);
if ((hr == HRESULT.S_OK))
{
FileNames.Add(sbResult.ToString());
}
}
}
}
}
}
}
else if (hr == HRESULT.E_CANCELLED)
return DialogResult.Cancel;
return DialogResult.OK;
}
finally
{
Marshal.ReleaseComObject(fod);
}
}
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern HRESULT SHCreateItemFromParsingName(string pszPath, IntPtr pbc, ref Guid riid, ref IntPtr ppv);
public const int MAX_PATH = 260;
public const uint ERROR_CANCELLED = 1223;
public enum HRESULT : uint
{
S_OK = 0,
S_FALSE = 1,
E_NOINTERFACE = 0x80004002,
E_NOTIMPL = 0x80004001,
E_FAIL = 0x80004005,
E_INVALIDARG = 0x80070057,
E_CANCELLED = 0x80070000 + ERROR_CANCELLED,
E_UNEXPECTED = 0x8000FFFF
}
private class DialogEventSink : IFileDialogEvents, IFileDialogControlEvents
{
private FileDialog parent;
public DialogEventSink(FileDialog fileDialog)
{
this.parent = fileDialog;
}
public uint Cookie { get; set; }
public HRESULT OnFileOk([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd)
{
return HRESULT.S_OK;
}
public HRESULT OnFolderChanging([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psiFolder)
{
//System.Text.StringBuilder sbResult = new System.Text.StringBuilder(MAX_PATH);
return HRESULT.S_OK;
}
// If defined as Function => "Attempted to read or write protected memory. This is often an indication that other memory is corrupt "
public void OnFolderChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd)
{
}
public void OnSelectionChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd)
{
}
public void OnShareViolation([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, out FDE_SHAREVIOLATION_RESPONSE pResponse)
{
//throw new NotImplementedException();
pResponse = FDE_SHAREVIOLATION_RESPONSE.FDESVR_DEFAULT;
}
public void OnTypeChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd)
{
//throw new NotImplementedException();
}
public void OnOverwrite([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, out FDE_OVERWRITE_RESPONSE pResponse)
{
//throw new NotImplementedException();
pResponse = FDE_OVERWRITE_RESPONSE.FDEOR_DEFAULT;
}
public HRESULT OnItemSelected([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl, [In] int dwIDItem)
{
//throw new NotImplementedException();
return HRESULT.S_OK;
}
// If defined as Function => "Attempted to read or write protected memory. This is often an indication that other memory is corrupt "
public void OnButtonClicked([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl)
{
//switch (dwIDCtl)
//{
// // defined in AddPushButton
// case 601:
// {
// break;
// }
// default:
// {
// break;
// }
//}
}
public HRESULT OnCheckButtonToggled([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl, [In] bool bChecked)
{
//throw new NotImplementedException();
return HRESULT.S_OK;
}
public HRESULT OnControlActivating([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl)
{
//throw new NotImplementedException();
return HRESULT.S_OK;
}
}
[ComImport]
[Guid("b4db1657-70d7-485e-8e3e-6fcb5a5c1802")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IModalWindow
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall, MethodCodeType = System.Runtime.CompilerServices.MethodCodeType.Runtime)]
[PreserveSig]
uint Show([In] IntPtr hwndOwner);
}
[ComImport]
[Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileDialog : IModalWindow
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall, MethodCodeType = System.Runtime.CompilerServices.MethodCodeType.Runtime)]
[PreserveSig]
new HRESULT Show([In] IntPtr parent);
HRESULT SetFileTypes([In] uint cFileTypes, [In][MarshalAs(UnmanagedType.LPArray)] COMDLG_FILTERSPEC[] rgFilterSpec);
HRESULT SetFileTypeIndex([In] uint iFileType);
HRESULT GetFileTypeIndex(out uint piFileType);
HRESULT Advise([In][MarshalAs(UnmanagedType.Interface)] IFileDialogEvents pfde, out uint pdwCookie);
HRESULT Unadvise([In] uint dwCookie);
HRESULT SetOptions([In] FOS fos);
HRESULT GetOptions(out FOS pfos);
HRESULT SetDefaultFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
HRESULT SetFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
HRESULT GetFolder([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
HRESULT GetCurrentSelection([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
HRESULT SetFileName([In][MarshalAs(UnmanagedType.LPWStr)] string pszName);
HRESULT GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
HRESULT SetTitle([In][MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
HRESULT SetOkButtonLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
HRESULT SetFileNameLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
[PreserveSig]
HRESULT GetResult([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
HRESULT AddPlace([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, FDAP fdap);
HRESULT SetDefaultExtension([In][MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
HRESULT Close([MarshalAs(UnmanagedType.Error)] int hr);
HRESULT SetClientGuid([In] ref Guid guid);
HRESULT ClearClientData();
HRESULT SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
}
public enum FDAP
{
FDAP_BOTTOM,
FDAP_TOP
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct COMDLG_FILTERSPEC
{
[MarshalAs(UnmanagedType.LPWStr)]
public string pszName;
[MarshalAs(UnmanagedType.LPWStr)]
public string pszSpec;
}
public enum FOS : uint
{
FOS_OVERWRITEPROMPT = 0x2,
FOS_STRICTFILETYPES = 0x4,
FOS_NOCHANGEDIR = 0x8,
FOS_PICKFOLDERS = 0x20,
FOS_FORCEFILESYSTEM = 0x40,
FOS_ALLNONSTORAGEITEMS = 0x80,
FOS_NOVALIDATE = 0x100,
FOS_ALLOWMULTISELECT = 0x200,
FOS_PATHMUSTEXIST = 0x800,
FOS_FILEMUSTEXIST = 0x1000,
FOS_CREATEPROMPT = 0x2000,
FOS_SHAREAWARE = 0x4000,
FOS_NOREADONLYRETURN = 0x8000,
FOS_NOTESTFILECREATE = 0x10000,
FOS_HIDEMRUPLACES = 0x20000,
FOS_HIDEPINNEDPLACES = 0x40000,
FOS_NODEREFERENCELINKS = 0x100000,
FOS_OKBUTTONNEEDSINTERACTION = 0x200000, // Only enable the OK button If the user has done something In the view.
FOS_DONTADDTORECENT = 0x2000000,
FOS_FORCESHOWHIDDEN = 0x10000000,
FOS_DEFAULTNOMINIMODE = 0x20000000, // (Not used In Win7)
FOS_FORCEPREVIEWPANEON = 0x40000000,
FOS_SUPPORTSTREAMABLEITEMS = 0x80000000 // Indicates the caller will use BHID_Stream To open contents, no need To download the file
}
[ComImport()]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
public interface IShellItem
{
HRESULT BindToHandler(IntPtr pbc, ref Guid bhid, ref Guid riid, ref IntPtr ppv);
HRESULT GetParent(ref IShellItem ppsi);
[PreserveSig]
HRESULT GetDisplayName(SIGDN sigdnName, ref System.Text.StringBuilder ppszName);
HRESULT GetAttributes(uint sfgaoMask, ref uint psfgaoAttribs);
HRESULT Compare(IShellItem psi, uint hint, ref int piOrder);
}
public enum SIGDN : uint
{
SIGDN_NORMALDISPLAY = 0x0,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004C000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_URL = 0x80068000,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007C001,
SIGDN_PARENTRELATIVE = 0x80080001
}
[ComImport]
[Guid("973510DB-7D7F-452B-8975-74A85828D354")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileDialogEvents
{
[PreserveSig]
HRESULT OnFileOk([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd);
[PreserveSig]
HRESULT OnFolderChanging([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psiFolder);
// If defined as Function => "Attempted to read or write protected memory. This is often an indication that other memory is corrupt "
void OnFolderChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd);
void OnSelectionChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd);
void OnShareViolation([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, out FDE_SHAREVIOLATION_RESPONSE pResponse);
void OnTypeChange([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd);
void OnOverwrite([In][MarshalAs(UnmanagedType.Interface)] IFileDialog pfd, [In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, out FDE_OVERWRITE_RESPONSE pResponse);
}
public enum FDE_SHAREVIOLATION_RESPONSE
{
FDESVR_DEFAULT = 0x0,
FDESVR_ACCEPT = 0x1,
FDESVR_REFUSE = 0x2
}
public enum FDE_OVERWRITE_RESPONSE
{
FDEOR_DEFAULT = 0x0,
FDEOR_ACCEPT = 0x1,
FDEOR_REFUSE = 0x2
}
[ComImport]
[Guid("36116642-D713-4b97-9B83-7484A9D00433")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFileDialogControlEvents
{
HRESULT OnItemSelected([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl, [In] int dwIDItem);
// If defined as Function => "Attempted to read or write protected memory. This is often an indication that other memory is corrupt "
void OnButtonClicked([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl);
HRESULT OnCheckButtonToggled([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl, [In] bool bChecked);
HRESULT OnControlActivating([In][MarshalAs(UnmanagedType.Interface)] IFileDialogCustomize pfdc, [In] int dwIDCtl);
}
[ComImport]
[Guid("e6fdd21a-163f-4975-9c8c-a69f1ba37034")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFileDialogCustomize
{
HRESULT EnableOpenDropDown([In] int dwIDCtl);
HRESULT AddMenu([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
HRESULT AddPushButton([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
HRESULT AddComboBox([In] int dwIDCtl);
HRESULT AddRadioButtonList([In] int dwIDCtl);
HRESULT AddCheckButton([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel, [In] bool bChecked);
HRESULT AddEditBox([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
HRESULT AddSeparator([In] int dwIDCtl);
HRESULT AddText([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
HRESULT SetControlLabel([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
HRESULT GetControlState([In] int dwIDCtl, out CDCONTROLSTATEF pdwState);
HRESULT SetControlState([In] int dwIDCtl, [In] CDCONTROLSTATEF dwState);
HRESULT GetEditBoxText([In] int dwIDCtl, [MarshalAs(UnmanagedType.LPWStr)] out string ppszText);
HRESULT SetEditBoxText([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
HRESULT GetCheckButtonState([In] int dwIDCtl, out bool pbChecked);
HRESULT SetCheckButtonState([In] int dwIDCtl, [In] bool bChecked);
HRESULT AddControlItem([In] int dwIDCtl, [In] int dwIDItem, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
HRESULT RemoveControlItem([In] int dwIDCtl, [In] int dwIDItem);
HRESULT RemoveAllControlItems([In] int dwIDCtl);
HRESULT GetControlItemState([In] int dwIDCtl, [In] int dwIDItem, out CDCONTROLSTATEF pdwState);
HRESULT SetControlItemState([In] int dwIDCtl, [In] int dwIDItem, [In] CDCONTROLSTATEF dwState);
HRESULT GetSelectedControlItem([In] int dwIDCtl, out int pdwIDItem);
HRESULT SetSelectedControlItem([In] int dwIDCtl, [In] int dwIDItem);
HRESULT StartVisualGroup([In] int dwIDCtl, [In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
HRESULT EndVisualGroup();
HRESULT MakeProminent([In] int dwIDCtl);
HRESULT SetControlItemText([In] int dwIDCtl, [In] int dwIDItem, [In] string pszLabel);
}
public enum CDCONTROLSTATEF
{
CDCS_INACTIVE = 0,
CDCS_ENABLED = 0x1,
CDCS_VISIBLE = 0x2,
CDCS_ENABLEDVISIBLE = 0x3
}
[ComImport]
[Guid("d57c7288-d4ad-4768-be02-9d969532d960")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileOpenDialog : IFileDialog
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall, MethodCodeType = System.Runtime.CompilerServices.MethodCodeType.Runtime)]
[PreserveSig]
new HRESULT Show([In] IntPtr hwndOwner);
new HRESULT SetFileTypes([In] uint cFileTypes, [In][MarshalAs(UnmanagedType.LPArray)] COMDLG_FILTERSPEC[] rgFilterSpec);
new HRESULT SetFileTypeIndex([In] uint iFileType);
new HRESULT GetFileTypeIndex(out uint piFileType);
new HRESULT Advise([In][MarshalAs(UnmanagedType.Interface)] IFileDialogEvents pfde, out uint pdwCookie);
new HRESULT Unadvise([In] uint dwCookie);
new HRESULT SetOptions([In] FOS fos);
new HRESULT GetOptions(out FOS pfos);
new HRESULT SetDefaultFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
new HRESULT SetFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
new HRESULT GetFolder([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT GetCurrentSelection([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT SetFileName([In][MarshalAs(UnmanagedType.LPWStr)] string pszName);
new HRESULT GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
new HRESULT SetTitle([In][MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
new HRESULT SetOkButtonLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
new HRESULT SetFileNameLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
[PreserveSig]
new HRESULT GetResult([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT AddPlace([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, FDAP fdap);
new HRESULT SetDefaultExtension([In][MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
new HRESULT Close([MarshalAs(UnmanagedType.Error)] int hr);
new HRESULT SetClientGuid([In] ref Guid guid);
new HRESULT ClearClientData();
new HRESULT SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
[PreserveSig]
HRESULT GetResults([MarshalAs(UnmanagedType.Interface)] out IShellItemArray ppenum);
HRESULT GetSelectedItems([MarshalAs(UnmanagedType.Interface)] out IShellItemArray ppsai);
}
[ComImport()]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b63ea76d-1f85-456f-a19c-48159efa858b")]
public interface IShellItemArray
{
HRESULT BindToHandler(IntPtr pbc, ref Guid bhid, ref Guid riid, ref IntPtr ppvOut);
HRESULT GetPropertyStore(GETPROPERTYSTOREFLAGS flags, ref Guid riid, ref IntPtr ppv);
HRESULT GetPropertyDescriptionList(PROPERTYKEY keyType, ref Guid riid, ref IntPtr ppv);
// Function GetAttributes(AttribFlags As SIATTRIBFLAGS, sfgaoMask As SFGAOF, ByRef psfgaoAttribs As SFGAOF) As HRESULT
HRESULT GetAttributes(SIATTRIBFLAGS AttribFlags, int sfgaoMask, ref int psfgaoAttribs);
HRESULT GetCount(ref int pdwNumItems);
HRESULT GetItemAt(int dwIndex, ref IShellItem ppsi);
// Function EnumItems(ByRef ppenumShellItems As IEnumShellItems) As HRESULT
HRESULT EnumItems(ref IntPtr ppenumShellItems);
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct PROPERTYKEY
{
private Guid fmtid;
private int pid;
public Guid FormatId
{
get
{
return this.fmtid;
}
}
public int PropertyId
{
get
{
return this.pid;
}
}
public PROPERTYKEY(Guid formatId, int propertyId)
{
this.fmtid = formatId;
this.pid = propertyId;
}
public static readonly PROPERTYKEY PKEY_DateCreated = new PROPERTYKEY(new Guid("B725F130-47EF-101A-A5F1-02608C9EEBAC"), 15);
}
public enum GETPROPERTYSTOREFLAGS
{
GPS_DEFAULT = 0,
GPS_HANDLERPROPERTIESONLY = 0x1,
GPS_READWRITE = 0x2,
GPS_TEMPORARY = 0x4,
GPS_FASTPROPERTIESONLY = 0x8,
GPS_OPENSLOWITEM = 0x10,
GPS_DELAYCREATION = 0x20,
GPS_BESTEFFORT = 0x40,
GPS_NO_OPLOCK = 0x80,
GPS_PREFERQUERYPROPERTIES = 0x100,
GPS_EXTRINSICPROPERTIES = 0x200,
GPS_EXTRINSICPROPERTIESONLY = 0x400,
GPS_MASK_VALID = 0x7FF
}
public enum SIATTRIBFLAGS
{
SIATTRIBFLAGS_AND = 0x1,
SIATTRIBFLAGS_OR = 0x2,
SIATTRIBFLAGS_APPCOMPAT = 0x3,
SIATTRIBFLAGS_MASK = 0x3,
SIATTRIBFLAGS_ALLITEMS = 0x4000
}
[ComImport]
[Guid("84bccd23-5fde-4cdb-aea4-af64b83d78ab")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileSaveDialog : IFileDialog
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall, MethodCodeType = System.Runtime.CompilerServices.MethodCodeType.Runtime)]
[PreserveSig]
new HRESULT Show([In] IntPtr hwndOwner);
new HRESULT SetFileTypes([In] uint cFileTypes, [In][MarshalAs(UnmanagedType.LPArray)] COMDLG_FILTERSPEC[] rgFilterSpec);
new HRESULT SetFileTypeIndex([In] uint iFileType);
new HRESULT GetFileTypeIndex(out uint piFileType);
new HRESULT Advise([In][MarshalAs(UnmanagedType.Interface)] IFileDialogEvents pfde, out uint pdwCookie);
new HRESULT Unadvise([In] uint dwCookie);
new HRESULT SetOptions([In] FOS fos);
new HRESULT GetOptions(out FOS pfos);
new HRESULT SetDefaultFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
new HRESULT SetFolder([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi);
new HRESULT GetFolder([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT GetCurrentSelection([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT SetFileName([In][MarshalAs(UnmanagedType.LPWStr)] string pszName);
new HRESULT GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
new HRESULT SetTitle([In][MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
new HRESULT SetOkButtonLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszText);
new HRESULT SetFileNameLabel([In][MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
[PreserveSig]
new HRESULT GetResult([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi);
new HRESULT AddPlace([In][MarshalAs(UnmanagedType.Interface)] IShellItem psi, FDAP fdap);
new HRESULT SetDefaultExtension([In][MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
new HRESULT Close([MarshalAs(UnmanagedType.Error)] int hr);
new HRESULT SetClientGuid([In] ref Guid guid);
new HRESULT ClearClientData();
new HRESULT SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
[PreserveSig]
HRESULT SetSaveAsItem(IShellItem psi);
HRESULT SetProperties(IPropertyStore pStore);
HRESULT SetCollectedProperties(IPropertyDescriptionList pList, bool fAppendDefault);
HRESULT GetProperties(out IPropertyStore ppStore);
HRESULT ApplyProperties(IShellItem psi, IPropertyStore pStore, IntPtr hwnd, IFileOperationProgressSink pSink);
}
[ComImport, Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPropertyStore
{
HRESULT GetCount([Out] out uint propertyCount);
HRESULT GetAt([In] uint propertyIndex, [Out, MarshalAs(UnmanagedType.Struct)] out PROPERTYKEY key);
HRESULT GetValue([In, MarshalAs(UnmanagedType.Struct)] ref PROPERTYKEY key, [Out, MarshalAs(UnmanagedType.Struct)] out PROPVARIANT pv);
HRESULT SetValue([In, MarshalAs(UnmanagedType.Struct)] ref PROPERTYKEY key, [In, MarshalAs(UnmanagedType.Struct)] ref PROPVARIANT pv);
HRESULT Commit();
}
[StructLayout(LayoutKind.Sequential)]
public struct PROPARRAY
{
public uint cElems;
public IntPtr pElems;
}
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct PROPVARIANT
{
[FieldOffset(0)]
public ushort varType;
[FieldOffset(2)]
public ushort wReserved1;
[FieldOffset(4)]
public ushort wReserved2;
[FieldOffset(6)]
public ushort wReserved3;
[FieldOffset(8)]
public byte bVal;
[FieldOffset(8)]
public sbyte cVal;
[FieldOffset(8)]
public ushort uiVal;
[FieldOffset(8)]
public short iVal;
[FieldOffset(8)]
public UInt32 uintVal;
[FieldOffset(8)]
public Int32 intVal;
[FieldOffset(8)]
public UInt64 ulVal;
[FieldOffset(8)]
public Int64 lVal;
[FieldOffset(8)]
public float fltVal;
[FieldOffset(8)]
public double dblVal;
[FieldOffset(8)]
public short boolVal;
[FieldOffset(8)]
public IntPtr pclsidVal;
[FieldOffset(8)]
public IntPtr pszVal;
[FieldOffset(8)]
public IntPtr pwszVal;
[FieldOffset(8)]
public IntPtr punkVal;
[FieldOffset(8)]
public PROPARRAY ca;
[FieldOffset(8)]
public System.Runtime.InteropServices.ComTypes.FILETIME filetime;
}
[ComImport, Guid("1f9fc1d0-c39b-4b26-817f-011967d3440e"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPropertyDescriptionList
{
HRESULT GetCount(out uint pcElem);
HRESULT GetAt(uint iElem, ref Guid riid, out IntPtr ppv);
}
[ComImport]
[Guid("04b0f1a7-9490-44bc-96e1-4296a31252e2")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFileOperationProgressSink
{
void StartOperations();
void FinishOperations(HRESULT hrResult);
void PreRenameItem(uint dwFlags, IShellItem psiItem, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName);
void PostRenameItem(uint dwFlags, IShellItem psiItem, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName, HRESULT hrRename, IShellItem psiNewlyCreated);
void PreMoveItem(uint dwFlags, IShellItem psiItem, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName);
void PostMoveItem(uint dwFlags, IShellItem psiItem, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName, HRESULT hrMove, IShellItem psiNewlyCreated);
void PreCopyItem(uint dwFlags, IShellItem psiItem, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName);
void PostCopyItem(uint dwFlags, IShellItem psiItem, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName, HRESULT hrCopy, IShellItem psiNewlyCreated);
void PreDeleteItem(uint dwFlags, IShellItem psiItem);
void PostDeleteItem(uint dwFlags, IShellItem psiItem, HRESULT hrDelete, IShellItem psiNewlyCreated);
void PreNewItem(uint dwFlags, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName);
void PostNewItem(uint dwFlags, IShellItem psiDestinationFolder, [MarshalAs(UnmanagedType.LPWStr)] string pszNewName, [MarshalAs(UnmanagedType.LPWStr)] string pszTemplateName, uint dwFileAttributes, HRESULT hrNew, IShellItem psiNewItem);
void UpdateProgress(uint iWorkTotal, uint iWorkSoFar);
void ResetTimer();
void PauseTimer();
void ResumeTimer();
}
[ComImport]
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
private class FileOpenDialog
{
}
[ComImport]
[Guid("C0B4E2F3-BA21-4773-8DBA-335EC946EB8B")]
private class FileSaveDialog
{
}
}
}
}
Hi Costorix31, Thanks for your code. If i have t change the destination folder from documents to any other location. How to achive that.
thanks
You must get an IShellItem, with SHCreateItemFromParsingName that I used in other parts of the test
It can be done with IFileOpenDialog to select a file and IFileOperation to copy it,
like in the comment I posted in this thread with a test with an Android phone (Archos brand)
Thanks Castorix31, It looks that will solve my problem.
I'm also looking to these packages from nuget https://www.nuget.org/packages/MediaDevices/ and https://www.nuget.org/packages/PortableDevices/1.0.1 .
Yes, I had tested WPD too (you can use it without package (as explained here for example)
but it does not seem to have a Dialog to select files
(if you use IFileOpenDialog and have problems I could repost/adapt the code I had posted before...)
If you have an example with IFileDialog that you could share it would be nice.
Based on the requirements you describe you do not need to write a program, you can use OneDrive, Google Drive or Dropbox.
The app will do other things. One of the requirements is to permit file transfer
Thanks
8 people are following this question.
Insert a node as child ,before or after a node in nested dynamic JSON Node using C#
Visual Studio 2019: Undefined behavior in a C++/CLI wrapper project.
Example for how to get Package Metadata from Azure DevOps Rest-Api Artifacts using c#
How to collapse individual nested grids/stackpanels inside a grid?