|
-
Jul 10th, 2012, 12:16 AM
#1
Thread Starter
Member
[RESOLVED] Open file in read-only (ShellExecute API)
guys how can i open a specific file in read-only state using ShellExecute? this is my code so far, but the SetAttr doesn't work. it opens the file but the user can edit it (coz it's not open in read-only)..
<code for Setting attributes>
Case "Open"
txtFile.Text = "\\192.168.0.215" + "\Public\iKnow attachments\" + frmDM.Label9.Caption
SetAttr txtfile.text,vbReadOnly
txtParameters.Text = ""
txtDirectory.Text = ""
cboShowStyle.ListIndex = STYLE_NORMAL
</code>
<code for opening file using ShellExecute>
result = ShellExecute(Me.hWnd, cboOperation.Text, txtFile.Text, txtParameters.Text, txtDirectory.Text, show_style)
</code>
need some help here...
-
Jul 10th, 2012, 06:30 AM
#2
Re: Open file in read-only (ShellExecute API)
This works
vb Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
(ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
SetFileAttributes "c:\MyFile.txt", FILE_ATTRIBUTE_READONLY
ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
End Sub
-
Jul 10th, 2012, 07:20 PM
#3
Re: Open file in read-only (ShellExecute API)
This does the same thing, perhaps quicker as well as shorter because it skips the overhead of Declare-based API calling:
Code:
SetAttr "c:\MyFile.txt", vbReadOnly
'Set reference to Microsoft Shell Controls and Automation:
With New Shell32.Shell
.ShellExecute "c:\MyFile.txt", , "c:\", , vbNormalFocus
End With
Neither one really "opens read-only" though. To do that you need to know what tool you're opening with and pass its command line parameters for a read-only open instead of using ShellExecute.
Last edited by dilettante; Jul 10th, 2012 at 07:23 PM.
-
Jul 10th, 2012, 08:37 PM
#4
Re: Open file in read-only (ShellExecute API)
It should be noted that Hack's code will clear any other attributes. You should avoid doing that; I've modified his example so that no other attributes are changed.
vb Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
(ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
Dim lAt As Long
lAt = GetFileAttributes("c:\MyFile.txt")
lAt = lAt And FILE_ATTRIBUTE_READONLY
SetFileAttributes "c:\MyFile.txt", lAt
ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
End Sub
-
Jul 11th, 2012, 12:43 AM
#5
Thread Starter
Member
Re: Open file in read-only (ShellExecute API)
thanks! i got it solved! thanks for all the concerns.. )
-
Jul 11th, 2012, 11:01 AM
#6
Re: Open file in read-only (ShellExecute API)
If you're concerned about just turning on the "read only" attribute:
Code:
SetAttr "c:\MyFile.txt", GetAttr("c:\MyFile.txt") Or vbReadOnly
Note the correct oepration is Or, not And which will wipe the other attributes.
-
Jul 11th, 2012, 08:40 PM
#7
Thread Starter
Member
Re: [RESOLVED] Open file in read-only (ShellExecute API)
thanks dilettante for the advice.. will consider it also..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|