Results 1 to 7 of 7

Thread: [RESOLVED] Open file in read-only (ShellExecute API)

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Resolved [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...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Open file in read-only (ShellExecute API)

    This works
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    4. (ByVal hwnd As Long, _
    5. ByVal lpOperation As String, _
    6. ByVal lpFile As String, _
    7. ByVal lpParameters As String, _
    8. ByVal lpDirectory As String, _
    9. ByVal nShowCmd As Long) As Long
    10.  
    11. Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
    12. (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
    13.  
    14. Private Const FILE_ATTRIBUTE_READONLY = &H1
    15.  
    16. Private Const SW_SHOWNORMAL = 1
    17.  
    18. Private Sub Command1_Click()
    19. SetFileAttributes "c:\MyFile.txt", FILE_ATTRIBUTE_READONLY
    20. ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
    21. End Sub

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    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:
    1. Option Explicit
    2.     Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    3.     (ByVal hwnd As Long, _
    4.     ByVal lpOperation As String, _
    5.     ByVal lpFile As String, _
    6.     ByVal lpParameters As String, _
    7.     ByVal lpDirectory As String, _
    8.     ByVal nShowCmd As Long) As Long
    9.     Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    10.     Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
    11.     (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
    12.     Private Const FILE_ATTRIBUTE_READONLY = &H1
    13.     Private Const SW_SHOWNORMAL = 1
    14.     Private Sub Command1_Click()
    15.     Dim lAt As Long
    16.     lAt = GetFileAttributes("c:\MyFile.txt")
    17.     lAt = lAt And FILE_ATTRIBUTE_READONLY
    18.     SetFileAttributes "c:\MyFile.txt", lAt
    19.     ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
    20.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Re: Open file in read-only (ShellExecute API)

    thanks! i got it solved! thanks for all the concerns.. )

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    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
  •  



Click Here to Expand Forum to Full Width