By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Using the SHFILEOPSTRUCT structure

P: n/a
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs
Below Filename is a string
With FileOperation

..wFunc = FO_DELETE

..pFrom = FileName??

..pTo = ??

..fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure


Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Hi,

Here is the structure, dll declare, and sample code. It will allow
you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As
Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

..wFunc = FO_DELETE

..pFrom = OpenFileDialog1.FileName

..fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

-----------------------------

" Just Me" <ne********@a-znet.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs
Below Filename is a string
With FileOperation

..wFunc = FO_DELETE

..pFrom = FileName??

..pTo = ??

..fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure

Nov 20 '05 #2

P: n/a
Ken,

* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
Here is the structure, dll declare, and sample code. It will allow
you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT


Notice that this structure has byte-aligned members, so I would specify
'Pack:=1' in the attributes.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #3

P: n/a
I'm going to try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero?
As I remember, it is a list of filenames separated by semicolons. Maybe this
takes the chance that the byte after the file name does not happen to be a
semicolon??

Also, is there some reason a C# declare of the function was given. I'll need
to convert to VB and hope there is no special reason it was given in C#

Also, I believe fAnyOperationsAborted is BOOL. why map to Integer instead
of Boolean?
Thanks very much

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,

Here is the structure, dll declare, and sample code. It will allow you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = OpenFileDialog1.FileName

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

-----------------------------

" Just Me" <ne********@a-znet.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs Below Filename is a string
With FileOperation

.wFunc = FO_DELETE

.pFrom = FileName??

.pTo = ??

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure


Nov 20 '05 #4

P: n/a
Is it needed because of the "short"?
Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure
Thanks
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
Ken,

* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
Here is the structure, dll declare, and sample code. It will allow
you to select a file to delete with an open file dialog and send it to the recycle bin.

Structure SHFILEOPSTRUCT


Notice that this structure has byte-aligned members, so I would specify
'Pack:=1' in the attributes.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #5

P: n/a
* " Just Me" <ne********@a-znet.com> scripsit:
Is it needed because of the "short"?


Yes. Use this instead:

\\\
<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Auto)> _
Private Structure SHFILEOPSTRUCT
Public hWnd As IntPtr
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Boolean
Public hNameMappings As IntPtr
Public lpszProgressTitle As String
End Structure
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #6

P: n/a
Ken, I replied but inadvertingly put it in the wrong place. This is a copy.
I'm going to try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero? As I remember, it is a list of filenames
separated by semicolons. Maybe this takes the chance that the byte after the
file name does not happen to be a semicolon??

Also, is there some reason a C# declare of the function was given. I'll need
to convert to VB and hope there is no special reason it was given in C#

Also, I believe fAnyOperationsAborted is BOOL. why map to Integer instead
of Boolean?
Thanks very much


"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,

Here is the structure, dll declare, and sample code. It will allow you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = OpenFileDialog1.FileName

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

-----------------------------

" Just Me" <ne********@a-znet.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs Below Filename is a string
With FileOperation

.wFunc = FO_DELETE

.pFrom = FileName??

.pTo = ??

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure


Nov 20 '05 #7

P: n/a
Ken, I replied twice but the note ends up under Herfried's note. Here's a
copy (revised).

I did try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero? As I remember, it is a list of filenames
separated by semicolons. Maybe this takes the chance that the byte after the
file name does not happen to be a semicolon??
Thanks very much
Also, when I tried it I get "Cannot delete file: Cannot read from source
file or disk." error.

I've included the code below:


Imports System.Runtime.InteropServices

Public Class Form1

Inherits System.Windows.Forms.Form

Public Const FO_DELETE As Integer = &H3

Public Const FOF_ALLOWUNDO As Integer = &H40

Public Const FOF_SIMPLEPROGRESS As Integer = &H100

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(128, 160)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(104, 40)

Me.Button1.TabIndex = 0

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As
Integer

End Function

Private Sub Button1_Click1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

..wFunc = FO_DELETE

..pFrom = OpenFileDialog1.FileName

..fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

End Class

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,

Here is the structure, dll declare, and sample code. It will allow you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = OpenFileDialog1.FileName

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

Nov 20 '05 #8

P: n/a

I wonder if you know the answer to the following:

I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero?

As I remember, it is a list of filenames separated by semicolons.

Maybe this takes the chance that the byte after the file name does not
happen to be a semicolon??
Nov 20 '05 #9

P: n/a
Hi,

The Structure should be public not private.

Ken
------------------
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
* " Just Me" <ne********@a-znet.com> scripsit:
Is it needed because of the "short"?


Yes. Use this instead:

\\\
<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Auto)> _
Private Structure SHFILEOPSTRUCT
Public hWnd As IntPtr
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Boolean
Public hNameMappings As IntPtr
Public lpszProgressTitle As String
End Structure
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #10

P: n/a
Hi,

You can use dll import with vb.net as well as c# so I posted a
vb.net version of the api. As for pFrom it should end with chr(0) not 0.
That is added automatically.

Ken
---------------
" Just Me" <ne********@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm going to try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero?
As I remember, it is a list of filenames separated by semicolons. Maybe this
takes the chance that the byte after the file name does not happen to be a
semicolon??

Also, is there some reason a C# declare of the function was given. I'll need
to convert to VB and hope there is no special reason it was given in C#

Also, I believe fAnyOperationsAborted is BOOL. why map to Integer instead
of Boolean?
Thanks very much

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,

Here is the structure, dll declare, and sample code. It will allow you to select a file to delete with an open file dialog and send it to the
recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = OpenFileDialog1.FileName

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

-----------------------------

" Just Me" <ne********@a-znet.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs Below Filename is a string
With FileOperation

.wFunc = FO_DELETE

.pFrom = FileName??

.pTo = ??

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure



Nov 20 '05 #11

P: n/a
* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
The Structure should be public not private.


Depends on where you use it, I think. Or did I miss something?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #12

P: n/a

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

You can use dll import with vb.net as well as c# so I posted a I figured that out as some as I ran your code and didn't get a build error.

vb.net version of the api. As for pFrom it should end with chr(0) not 0.
That is added automatically. But the doc says there should be two NULLs at the end, not one.
It's how to get the extra zero that I wonder about.

The code I posted at 11:37 (really yours) always gives me an error when I
try to delete a file.

Thanks for all the answers

Ken
---------------
" Just Me" <ne********@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm going to try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is terminated with an extra zero?
As I remember, it is a list of filenames separated by semicolons. Maybe this takes the chance that the byte after the file name does not happen to be a
semicolon??

Also, is there some reason a C# declare of the function was given. I'll need to convert to VB and hope there is no special reason it was given in C#

Also, I believe fAnyOperationsAborted is BOOL. why map to Integer instead
of Boolean?
Thanks very much

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,

Here is the structure, dll declare, and sample code. It will

allow
you to select a file to delete with an open file dialog and send it to the recycle bin.

Structure SHFILEOPSTRUCT

Dim hwnd As Integer

Dim wFunc As Integer

Dim pFrom As String

Dim pTo As String

Dim fFlags As Short

Dim fAnyOperationsAborted As Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure

<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT)

As
Integer

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = OpenFileDialog1.FileName

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

End If

End Sub

Ken

-----------------------------

" Just Me" <ne********@a-znet.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Can anyone fix the code below?

I need to set pTo to NULL and pFrom to a fullfilepath followed by two

NULLs
Below Filename is a string
With FileOperation

.wFunc = FO_DELETE

.pFrom = FileName??

.pTo = ??

.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS

End With

My guess for the structure:

Public Structure SHFILEOPSTRUCT

Public hwnd As IntPtr

Public wFunc As Integer

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pFrom As String

<VB.VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> _

Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure



Nov 20 '05 #13

P: n/a
To both MVP's,

"I fixed it. I'd appreciate comments on when it's OK to just use String in
a Structure and when Marshal must be used.
This is what I did to the structure:

<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Auto)> _

Public Structure SHFILEOPSTRUCT

Public hWnd As IntPtr

Public wFunc As Integer

<MarshalAs(UnmanagedType.LPStr)> Public pFrom As String

<MarshalAs(UnmanagedType.LPStr)> Public pTo As String

'Public pFrom As String

'Public pTo As String

Public fFlags As Short

Public fAnyOperationsAborted As Boolean

Public hNameMappings As IntPtr

Public lpszProgressTitle As String

End Structure

I also had to add Chr(0) as follows:

..pFrom = OpenFileDialog1.FileName & Chr(0)

but that I understand.

Thansk for the help.


Nov 20 '05 #14

P: n/a
* " Just Me" <ne********@a-znet.com> scripsit:
I also had to add Chr(0) as follows:

.pFrom = OpenFileDialog1.FileName & Chr(0)


.... or 'ControlChars.NullChar', 'vbNullChar'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.