Createfile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Createfile

  1. #1
    Join Date
    Sep 2004
    Posts
    30

    Createfile

    Hi!
    I have some trouble.. I have this old vb6 code that I want to use with vb2005. It simply lock a folder or a file from other users.
    I just cant figure it out.
    Any one??

    Module:
    Private Const FILE_LIST_DIRECTORY = &H1
    Private Const FILE_SHARE_READ = &H1&
    Private Const FILE_SHARE_DELETE = &H4&
    Private Const OPEN_EXISTING = 3
    Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal PassZero As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal PassZero As Long) As Long

    Form:
    Dim File_Share_Flag As Long
    Dim hDir As Long
    Private Sub Command1_Click()
    Dim sDirOrFile as string
    sDirOrFile = "C:\Myfile.txt"
    hDir = CreateFile(sDirOrFile, FILE_LIST_DIRECTORY, File_Share_Flag, _
    ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
    LockFolderOrFile = hDir
    End Sub

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Why not simply use the System.IO.File.Create method?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Sep 2004
    Posts
    30
    Thanks Mr Phil Weber for youre reply!
    That works fine! But I dont understand how to close the file.
    I want to be able to close the file without exiting my program??
    I use:
    System.IO.File.Open("C:\MyFile.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)

  4. #4
    Join Date
    Sep 2004
    Posts
    30

    How do I close a file vb.net

    Hi all!
    Im having some problems with closing files.
    Im using FileShare.None because I dont want any user to
    read the file when my app using it. When my app is ready with the file,
    I mant my app to close the file making other user able to read the file.
    Without ending my program.

    I use:

    File.Open("C:\MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None)

    But how to close this file without ending my program???

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Call the .Close or .Dispose method on the variable to which you're assigning the result of File.Open. For example:

    Dim sr As StreamReader = File.Open(...)
    ' work with file
    sr.Close()
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  6. #6
    Join Date
    Sep 2004
    Posts
    30
    Thank you for replying!
    I cant get it to work. I only get this blue line under saying:
    "Value of type 'Sytem.IO.FileStream' can not be converted to 'System.IO.StreamReader.
    I Pasted my try:

    Imports System
    Imports System.IO

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sr As StreamReader = File.Open("C:\MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None)
    ' work with file
    sr.Close()
    End Sub
    End Class

  7. #7
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    How were you reading from the file before you changed it to use a StreamReader? Please post the exact File.Open line you were using.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  8. #8
    Join Date
    Sep 2004
    Posts
    30
    Thansk for your'e patiens with me!
    I just skipped the StreamReader and used this instead:

    Imports System.IO
    FileOpen(1, "C:\AA\Image1.gif", OpenMode.Input, OpenAccess.Read, OpenShare.LockRead)
    ' Do stuff
    FileClose(1)

    It feels more as vb6. I do have a little problems with vb 2005.
    Like adding nodes to the TreeView control. Dont know how many hours I spent just trying to get it right and I still can get it to work! Its not like vb6...

Similar Threads

  1. CreateFile & DeviceIoControl
    By Daniel Teufert in forum VB Classic
    Replies: 0
    Last Post: 06-03-2002, 06:15 AM
  2. CreateFile, writeFile API
    By Hong in forum VB Classic
    Replies: 0
    Last Post: 11-05-2001, 02:18 PM
  3. Replies: 1
    Last Post: 02-14-2001, 07:32 PM
  4. HOw to use CreateFile() in windows9x?
    By Marginean Lucian in forum VB Classic
    Replies: 2
    Last Post: 03-23-2000, 06:48 AM
  5. HOw to use CreateFile() in windows9x?
    By Marginean Lucian in forum VB Classic
    Replies: 0
    Last Post: 03-23-2000, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center