SHFileOperation problem

M

mt

How can i use the "SHFileOperation" 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(LayoutKind.Sequential)> _
Public Structure SHFILEOPSTRUCT
Public hwnd As Long
Public wFunc As Long
Public pFrom As String
Public pTo As String
Public fFlags As Integer
Public fAnyOperationsAborted As Boolean
Public hNameMappings As Long
Public lpszProgressTitle As String
End Structure

Public Declare Function SHFileOperation Lib "shell32.dll" Alias
"SHFileOperationA" (ByVal lpFileOp As SHFILEOPSTRUCT) As Long

Public Function DeleteFile(ByVal 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
 
?

=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=

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 "SHFileOperationW" ....
- SHFILEOPSTRUCT should be re-writed converting values to Long; String values should have the attribute <MarshalAs(UnmanagedType.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" <[email protected]'@nospam> escribió en el mensaje | How can i use the "SHFileOperation" 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(LayoutKind.Sequential)> _
| Public Structure SHFILEOPSTRUCT
| Public hwnd As Long
| Public wFunc As Long
| Public pFrom As String
| Public pTo As String
| Public fFlags As Integer
| Public fAnyOperationsAborted As Boolean
| Public hNameMappings As Long
| Public lpszProgressTitle As String
| End Structure
|
| Public Declare Function SHFileOperation Lib "shell32.dll" Alias
| "SHFileOperationA" (ByVal lpFileOp As SHFILEOPSTRUCT) As Long
|
| Public Function DeleteFile(ByVal 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
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top