473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SHFileOperation problem

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

Jul 19 '05 #1
1 3452
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" <mt*****@msn.com'@nospam> escribió en el mensaje news:uT**************@tk2msftngp13.phx.gbl...
| 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
|
|
|
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: avishay | last post by:
Hi All, I want to use SHFileOperation using Python and Win32 extentions, in order to move a file to the trash can. The function itself can be accessed by importing win32com.shell.shell. However, I...
1
by: John Ericson | last post by:
I am using SHFileOperation() to copy files from the local hard drive a network share using a mapped drive. I want to set the up as a batch process with no user intervention. I have using the fFlags...
2
by: Lespaul36 | last post by:
Is there a way to use shfileoperation to delete an entire folder instead of just the files?
2
by: RichardF | last post by:
I have a simple function to copy a file. If I call this with the following paramerters... psFrom = C:\12345678901234567890.abc psTo = C:\12345678901234567890.xyz.abc The TO file ends up...
2
by: mt | last post by:
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...
1
by: David S. | last post by:
Hi, I have a program that copy files using SHFileOperation, but my problem begins when i want copy files from another user. How can i do to copy files if i know the user and password? Thanks
0
by: =?Utf-8?B?TkVXMi5ORVQ=?= | last post by:
I have a .NET component (VC++) with native C++ at it's core. The native C++ code makes a call to SHFileOperation to delete a directory and it's contents. When I host the .NET component in a VB...
0
by: narsys | last post by:
I'm using SHFileOperation in my project. Everything is ok but something goes wrong when I want to use FOF_SIMPLEPROGRESS falg. when I set it it doesn't work. I can't see nothing! if I use ansi...
1
by: sambarker123 | last post by:
Hi all I am tring to copy a file.The code is as below int _tmain(int argc, _TCHAR* argv) { string s3,s4; s3="C:\\test\\to"; s4="C:\\test\\from\\sam.txt";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.