SHFileOperation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mt

    #1

    SHFileOperation problem

    How can i use the "SHFileOperatio n" with vb.net.
    i use the below code; but i get the error " Object reference not set to an
    instance of an object"
    please advise
    (and can i use this api for multiple file operations?)

    Public Const FO_DELETE = &H3
    Public Const FOF_ALLOWUNDO = &H40
    Public Const FOF_NOALLOWUNDO = 0
    Public Const FO_MOVE = &H1
    Public Const FO_COPY = &H2
    Public Const FO_RENAME = &H4

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure SHFILEOPSTRUCT
    Public hwnd As Long
    Public wFunc As Long
    Public pFrom As String
    Public pTo As String
    Public fFlags As Integer
    Public fAnyOperationsA borted As Boolean
    Public hNameMappings As Long
    Public lpszProgressTit le As String
    End Structure

    Public Declare Function SHFileOperation Lib "shell32.dl l" Alias
    "SHFileOperatio nA" (ByVal lpFileOp As SHFILEOPSTRUCT) As Long

    Public Function DeleteFile(ByVa l FileToDelete As String) As Long
    Dim FileOperation As New SHFILEOPSTRUCT( )
    With FileOperation
    ..wFunc = FO_DELETE
    ..pFrom = FileToDelete
    ..fFlags = &H40
    End With
    DeleteFile = SHFileOperation (FileOperation)
    End Function



  • José Manuel Agüero

    #2
    Re: SHFileOperation problem

    Hello, mt:

    Why don't you use the System.IO namespace? It's easier and safer.

    For the SHFileOperation you need some modifications:

    - SHFileOperation returns an Integer, not a Long.
    - You should declare it as Ansi: Declare Ansi Function ...
    - Or as Unicode: Declare Unicode Function ... Alias "SHFileOperatio nW" ....
    - SHFILEOPSTRUCT should be re-writed converting values to Long; String values should have the attribute <MarshalAs(Unma nagedType.LPStr )> (LPWStr for the Unicode version)
    - You have to include a reference to System.Runtime. InteropServices in order to have access to the MarshalAs property.
    - Maybe more things I don't see now...

    I think it's quite easier to use the System.IO namespace...

    Regards.


    "mt" <mtarkan@msn.co m'@nospam> escribió en el mensaje news:uTwtLH5cDH A.2932@tk2msftn gp13.phx.gbl...
    | How can i use the "SHFileOperatio n" with vb.net.
    | i use the below code; but i get the error " Object reference not set to an
    | instance of an object"
    | please advise
    | (and can i use this api for multiple file operations?)
    |
    | Public Const FO_DELETE = &H3
    | Public Const FOF_ALLOWUNDO = &H40
    | Public Const FOF_NOALLOWUNDO = 0
    | Public Const FO_MOVE = &H1
    | Public Const FO_COPY = &H2
    | Public Const FO_RENAME = &H4
    |
    | <StructLayout(L ayoutKind.Seque ntial)> _
    | Public Structure SHFILEOPSTRUCT
    | Public hwnd As Long
    | Public wFunc As Long
    | Public pFrom As String
    | Public pTo As String
    | Public fFlags As Integer
    | Public fAnyOperationsA borted As Boolean
    | Public hNameMappings As Long
    | Public lpszProgressTit le As String
    | End Structure
    |
    | Public Declare Function SHFileOperation Lib "shell32.dl l" Alias
    | "SHFileOperatio nA" (ByVal lpFileOp As SHFILEOPSTRUCT) As Long
    |
    | Public Function DeleteFile(ByVa l FileToDelete As String) As Long
    | Dim FileOperation As New SHFILEOPSTRUCT( )
    | With FileOperation
    | .wFunc = FO_DELETE
    | .pFrom = FileToDelete
    | .fFlags = &H40
    | End With
    | DeleteFile = SHFileOperation (FileOperation)
    | End Function
    |
    |
    |

    Comment

    Working...