shfileoperation

RonR

Well-known member
Joined
Nov 23, 2007
Messages
82
Programming Experience
5-10
VB.NET:
Imports System.Runtime.InteropServices


Inherits System.Windows.Forms.Form

    <StructLayout(LayoutKind.Sequential)> Private Structure SHFILEOPSTRUCT

        Dim hwnd As Integer
        Dim wFunc As Integer
        Dim pFrom As String
        <MarshalAs(UnmanagedType.LPStr)> Dim pTo As String
        Dim fFlags As Short
        Dim fAnyOperationsAborted As Boolean
        Dim hNameMappings As Integer
        <MarshalAs(UnmanagedType.LPStr)> Dim lpszProgressTitle As String

    End Structure



    Private Const FO_copy As Integer = &H2
    Private Const FOF_simpleprogress As Integer = &H100
    Private Const FOF_NoConfirmation As Integer = &H10
    Private Const FO_delete As Integer = &H3
    Private Const FOF_allowundo As Integer = &H40
    Private Const FOF_filesonly As Integer = &H80
    Private Const fof_multidestfiles As Integer = &H1

    Dim X As SHFILEOPSTRUCT

    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click


        X.pFrom = "c:\Mydata\*.*"

        X.pTo = "c:\copytest"

        X.fFlags = FOF_NoConfirmation

        X.wFunc = FO_copy

        SHFileOperation(X)

    End Sub



it will not accept *.*


it tells me it cannot read from the source file or disk.


it works on individual files or complete folder.


any ideas????
 
Last edited by a moderator:
Try this:
VB.NET:
My.Computer.FileSystem.CopyDirectory("C:\source", "C:\target", UIOption.AllDialogs)
 
I might have had some import I didn't notice that should qualify that enumeration value better. What you can do is check the options the IDE give you for third parameter of that method overload for the UIOption value. Actually for another overload of CopyDirectory method you also don't need any third parameter, I'm sure.
 
I am sorry but I have no idea what you are talking about.


where do I find the items you are saying?
 
View - Object Browser.

Where it says <search>, type in "CopyFile" and press Enter (or click the green arrow on the right)

Click the third option down, and look at the details in the centre window.
 
I tried this and it still tells me that UIoption is not declared

Public Enum UIOption As Integer
 
When I try it in my VS2005 it shows :-

VB.NET:
Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String, ByVal showUI As Microsoft.VisualBasic.FileIO.UIOption)
 
The references I have set are :-

System
System.Drawing
System.Windows.Forms

I always thought the Object Browser was reference-independent :confused:
 
Ron, I think you are missing the point. In the Object Browser, it shows you how to declare it - but you still have to do it for yourself.

VB.NET:
Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String, ByVal showUI As Microsoft.VisualBasic.FileIO.UIOption)

which means John's code earlier either needs changing to

VB.NET:
My.Computer.FileSystem.CopyDirectory("C:\source", "C:\target", Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)

or

VB.NET:
Imports Microsoft.VisualBasic.FileIO

...

My.Computer.FileSystem.CopyDirectory("C:\source", "C:\target", UIOption.AllDialogs)
 
this is the code that fixed it. I did not see the code in anything I checked.


Imports Microsoft.VisualBasic.FileIO



thanks
 
that was a big help, it gives me 75% of what I need.

I need one now that will allow me to select files with specific extensions.
 
I just pressed comma key to the third parameter and the IDE presented me with everything the CopyDirectory could do. When selecting the method overload that takes a UIOption and pressing space key the IDE displays all values from that enumeration. As I said, some other code (probably an automatic code snippet) must have imported the namespace, but that really is irrelevant since IDE will generate the correct code no matter which imports you have, all you have to do is navigate the intellisense boxes as you write code.
 
Back
Top