0

hello all of you !
i am making a simple form which can create the text file from the database table , for this , i am using this code at the load event of my form so that it can create the files at the load time.

            IO.File.Create("C:\Customers.txt")
            IO.File.Create("C:\Towns.txt")
            IO.File.Create("C:\Products.txt")

this code is working fine , but when i try to write my data in these file then the error come which is
"the process cannot access the file 'c:\towns.txt' because it is used by another process"
i dont know where this file is used by my computer , Please help me in this as i am stuck here , if you have somthing better then this .please tell me

Regards

4
Contributors
5
Replies
7
Views
4 Years
Discussion Span
Last Post by M.Waqas Aslam
0

As soon as u create the file, it is in open state....u need to close the object of the file and then try to load the data...

FileClose()

Edited by poojavb

0

thanks pooja , !
i am now using this code

    IO.File.Create("C:\Towns.txt")
            FileClose()
            IO.File.Create("C:\Customers.txt")
            FileClose()
            IO.File.Create("C:\Products.txt")
            FileClose()

but still i got same error , :(

Regards

1

Hi,

I think this is a better way to create a textfile:

Dim fs As New FileStream("Towns.txt", FileMode.Create, FileAccess.Write)
        'declaring a FileStream and creating a text file named Towns with 
        'access mode of writing
        Dim s As New StreamWriter(fs)
        'creating a new StreamWriter and passing the filestream object fs as argument

        s.Close()
        'closing the file
Votes + Comments
Nice and Perfect
0

i am now using this code
but still i got same error , :(

Look at the return value of File.Create--it returns a FileStream object. If you want to close the file immediately, you need to do something like this:

FileStream fs = File.Create("fred.txt");
fs.Close();

Or if you're into the whole brevity thing:

File.Create("barney.txt").Close();

If you're going to be writing to it pretty soon after, though, it's better to keep the stream object around and just write to it later.

0

thank you so so very much both of you , pooja this time your code worked perfectly ,

Regards

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.