Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Createfile

Hi! :WAVE:
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
[1101 byte] By [jojo] at [2007-11-11 10:00:53]
# 1 Re: Createfile
Why not simply use the System.IO.File.Create method?
Phil Weber at 2007-11-11 21:45:01 >
# 2 Re: Createfile
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)
jojo at 2007-11-11 21:46:07 >
# 3 Re: Createfile
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?
jojo at 2007-11-11 21:47:06 >
# 4 Re: Createfile
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 at 2007-11-11 21:48:14 >
# 5 Re: Createfile
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
jojo at 2007-11-11 21:49:13 >
# 6 Re: Createfile
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 at 2007-11-11 21:50:07 >
# 7 Re: Createfile
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...
jojo at 2007-11-11 21:51:11 >