473,383 Members | 1,785 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,383 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 3450
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";
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.