Results 1 to 7 of 7

Thread: how can I lock a file?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I lock a file?

    umm I don't want the user to be able to move/delete a certain file when my app is running. Is there a way to "lock" a certain file without having it opened?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't think so not without having it opened.

  3. #3
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brisbane Australia
    Posts
    150

    lock file

    in VB6 you could open it as a random file, record length 1 character and LOCK #fn,1. Anyone who tries to access the file gets an error. CAN you do something similar in VB.NET ??

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, thats the same solution you would use in Vb.Net

    http://www.vbwm.com/articles/builder...p?ArticleID=15
    VB Code:
    1. ' attempts to open a file C:\TEST.TXT for read and write' access, and locks the file from other processes.
    2. Dim filename As String = "C:\TEST.TXT"
    3. Dim myFileStream As System.IO.FileStream
    4. Try   ' attempt to open the file  myFileStream = New   System.IO.FileStream(filename, _                       System.IO.FileMode.Open, _                       System.IO.FileAccess.ReadWrite, _                       System.IO.FileShare.None)
    5.  
    6. Catch   ' display an error to the user
    7. MessageBox.Show("Could not open file. Make sure " &              filename & "exists.")
    8. Finally
    9. If myFileStream.CanRead Then  
    10.   ' read from the file here
    11.  
    12.   ' close the file when you are done
    13.     myFileStream.Close()
    14. End If
    15.  
    16. End Try

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    well yeah I can open the file... I just didnt know if I should leave the file open or not. Sounds like I have to leave it open in order to lock the file....
    btw you used FileStream in your code and I always use StreamReader and StreamWriter. What's the big difference?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    well yeah I can open the file... I just didnt know if I should leave the file open or not. Sounds like I have to leave it open in order to lock the file....
    btw you used FileStream in your code and I always use StreamReader and StreamWriter. What's the big difference?
    from Microsoft Documentation..
    StreamReader
    Implements a TextReader that reads characters from a byte stream in a particular encoding

    StreamWriter
    Implements a TextWriter for writing characters to a stream in a particular encoding.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pirate
    from Microsoft Documentation..
    hmm so it sounds like that they are for text files
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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