Results 1 to 4 of 4

Thread: Edit a file-type (eg. Read-Only)

  1. #1

    Thread Starter
    Addicted Member Mars-Martian's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    185

    Edit a file-type (eg. Read-Only)

    I am making a program that will use a config file. It however needs to edit the filetype to read-only after it saves it.
    Obviously programmers can easily edit it. (Clicking read-only I NEVER THOUGHT OF THAT lol :P) However this program is mostly for..... n00bs.
    First person to be able to get what song is currently playing in Winamp 3.

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    From All Api Net's, API Guide :

    VB Code:
    1. Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long

    · lpFileName
    Points to a string that specifies the name of the file whose attributes are to be set.
    Windows 95: This string must not exceed MAX_PATH characters.
    Windows NT: There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the SetFileAttributes function parses paths. An application can transcend this limit and send in paths longer than MAX_PATH characters by calling the wide (W) version of SetFileAttributes and prepending “\\?\” to the path. The “\\?\” tells the function to turn off path parsing; it lets paths longer than MAX_PATH be used with SetFileAttributesW. This also works with UNC names. The “\\?\” is ignored as part of the path. For example, “\\?\C:\myworld\private” is seen as “C:\myworld\private”, and “\\?\UNC\wow\hotstuff\coolapps” is seen as “\\wow\hotstuff\coolapps”.

    · dwFileAttributes
    Specifies the file attributes to set for the file. This parameter can be a combination of the following values. However, all other values override FILE_ATTRIBUTE_NORMAL.
    FILE_ATTRIBUTE_ARCHIVE
    The file is an archive file. Applications use this value to mark files for backup or removal.
    FILE_ATTRIBUTE_HIDDEN
    The file is hidden. It is not included in an ordinary directory listing.
    FILE_ATTRIBUTE_NORMAL
    The file has no other attributes set. This value is valid only if used alone.
    FILE_ATTRIBUTE_OFFLINE
    The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
    FILE_ATTRIBUTE_READONLY
    The file is read-only. Applications can read the file but cannot write to it or delete it.
    FILE_ATTRIBUTE_SYSTEM
    The file is part of the operating system or is used exclusively by it.
    FILE_ATTRIBUTE_TEMPORARY
    The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.
    Usage:

    VB Code:
    1. Const MOVEFILE_REPLACE_EXISTING = &H1
    2. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    3. Const FILE_BEGIN = 0
    4. Const FILE_SHARE_READ = &H1
    5. Const FILE_SHARE_WRITE = &H2
    6. Const CREATE_NEW = 1
    7. Const OPEN_EXISTING = 3
    8. Const GENERIC_READ = &H80000000
    9. Const GENERIC_WRITE = &H40000000
    10. Private Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long
    11. Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
    12. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
    13. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    14. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    15. Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
    16. Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
    17. Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    18. Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
    19. Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
    20. Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    21. Private Sub Form_Load()
    22.     'KPD-Team 1998
    23.     'URL: [url]http://www.allapi.net/[/url]
    24.     'E-Mail: [email][email protected][/email]
    25.     Dim sSave As String, hOrgFile As Long, hNewFile As Long, bBytes() As Byte
    26.     Dim sTemp As String, nSize As Long, Ret As Long
    27.     'Ask for a new volume label
    28.     sSave = InputBox("Please enter a new volume label for drive C:\" + vbCrLf + " (if you don't want to change it, leave the textbox blank)")
    29.     If sSave <> "" Then
    30.         SetVolumeLabel "C:\", sSave
    31.     End If
    32.  
    33.     'Create a buffer
    34.     sTemp = String(260, 0)
    35.     'Get a temporary filename
    36.     GetTempFileName "C:\", "KPD", 0, sTemp
    37.     'Remove all the unnecessary chr$(0)'s
    38.     sTemp = Left$(sTemp, InStr(1, sTemp, Chr$(0)) - 1)
    39.     'Set the file attributes
    40.     SetFileAttributes sTemp, FILE_ATTRIBUTE_TEMPORARY
    41.     'Open the files
    42.     hNewFile = CreateFile(sTemp, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    43.     hOrgFile = CreateFile("c:\config.sys", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    44.  
    45.     'Get the file size
    46.     nSize = GetFileSize(hOrgFile, 0)
    47.     'Set the file pointer
    48.     SetFilePointer hOrgFile, Int(nSize / 2), 0, FILE_BEGIN
    49.     'Create an array of bytes
    50.     ReDim bBytes(1 To nSize - Int(nSize / 2)) As Byte
    51.     'Read from the file
    52.     ReadFile hOrgFile, bBytes(1), UBound(bBytes), Ret, ByVal 0&
    53.     'Check for errors
    54.     If Ret <> UBound(bBytes) Then MsgBox "Error reading file ..."
    55.  
    56.     'Write to the file
    57.     WriteFile hNewFile, bBytes(1), UBound(bBytes), Ret, ByVal 0&
    58.     'Check for errors
    59.     If Ret <> UBound(bBytes) Then MsgBox "Error writing file ..."
    60.  
    61.     'Close the files
    62.     CloseHandle hOrgFile
    63.     CloseHandle hNewFile
    64.  
    65.     'Move the file
    66.     MoveFileEx sTemp, "C:\KPDTEST.TST", MOVEFILE_REPLACE_EXISTING
    67.     'Delete the file
    68.     DeleteFile "C:\KPDTEST.TST"
    69.     Unload Me
    70. End Sub

    If you don't have the api guide yet - follow the link above to get it. It is a great tool!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    VB Code:
    1. SetAttr uFileName, vbReadOnly Or vbArchive

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  4. #4
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by KayJay
    VB Code:
    1. SetAttr uFileName, vbReadOnly Or vbArchive
    gee, I guess I made that one more complicated that it needed to be!

    hmmm, SetAttr - I just learned a new function
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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