Having a hard time trying to open a text file

T

Tony Girgenti

Hello.

I developed a web consuming windows application/form program with VS.NET
2003, VB, .Net Framework 1.1.4322, ASP.NET 1.1.4322, IIS 5.1.

Using this code to open a simple text file. If i get an error in my program
after the file has been opened once, it gives me a message saying
"System.IO.IOException: File already open."

FileOpen(1, "C:\xmltrips.txt", OpenMode.Output, OpenAccess.Write)

WriteLine(1, txtFilStr)

When i try to delete it, it says "It is being used by another person or
program".

If it's being opened output, then why won't it open again and again ?

I have a FileClose(1) in the catch statement.

Any help would be gratefully appreciated.

Thanks,
Tony
 
J

John Saunders

Tony Girgenti said:
Hello.

I developed a web consuming windows application/form program with VS.NET
2003, VB, .Net Framework 1.1.4322, ASP.NET 1.1.4322, IIS 5.1.

Using this code to open a simple text file. If i get an error in my
program after the file has been opened once, it gives me a message saying
"System.IO.IOException: File already open."

FileOpen(1, "C:\xmltrips.txt", OpenMode.Output, OpenAccess.Write)

WriteLine(1, txtFilStr)

When i try to delete it, it says "It is being used by another person or
program".

If it's being opened output, then why won't it open again and again ?

I have a FileClose(1) in the catch statement.

Any help would be gratefully appreciated.

What are FileOpen and FileClose? I don't recognize them

John
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

Using this code to open a simple text file. If i get an error in my
program after the file has been opened once, it gives me a message saying
"System.IO.IOException: File already open."

Thru a web-service?

Well, probably, multiple requests on the Web-Service is causing the error.

I would suggest using database rather than files to avoid concurrency
issues.



--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

If i use a database, how would i get the data from the database to the
mainframe ?

Same way as you would do from the file.
Or do you have the file on the MainFrame?

If so, you may install MSDE on the machine with .Net.
Make a simple table with only one field -- say, contents of type, may
be, blob.
Push all the contents in field as what you would push in the file.

This is the simplest way that you may leave the file and use database.
If the content in file is structured, you may created more fields in
database.


--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------
 
T

Tony Girgenti

I guess my point is, if i can't use a file in my web application to capture
the data during program operation, then how will i create and use a file in
my web application taking the data from the database. In other words,
what's the difference whether i create the file during program operation or
whether it creates the file afterwards reading from the database ?

Thanks,
Tony

"Gaurav Vaish (www.EduJiniOnline.com)"
 
E

ewpatton

You may want to try including OpenShare.Shared when you make your
FileOpen() call. The system may grant you shared access to the file,
depending on what the other process is that has previously accessed it.
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

I guess my point is, if i can't use a file in my web application to capture
the data during program operation, then how will i create and use a file in
my web application taking the data from the database. In other words,
what's the difference whether i create the file during program operation or
whether it creates the file afterwards reading from the database ?

Oh!
I thought the otherwise.... that you are using file to read the data from
:)

In that case, you may create a temporary file with a random name.
System.IO.Path.GetTempFileName method

Push all open/write in a try block.
Have the 'Close' in 'finally' block.

HTH

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------
 
T

Tony Girgenti

Gaurav.

Now that i have spent two days trying to figure out how to use a
dataset/database to put my data into, i'm not sure i want to go back to
using a flat text file.

Does thae fact that GetTempFileName has the word "Temp" in it tell me
anything. Should i wonder if the file will remain after the program
terminates ?

Thanks,
Tony

"Gaurav Vaish (www.EduJiniOnline.com)"
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

Does thae fact that GetTempFileName has the word "Temp" in it tell me
anything. Should i wonder if the file will remain after the program
terminates ?

Yes.
The folder in which it is created is always Path.GetTempPath
The name is randomly chosen so that it is unique.
The extension is always .tmp
The prefix is always tmp
The file is not temporary in the sense that it is not automatically deleted.


The method is a wrapper around the native method GetTempFileName.
The details of the native method is available at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/gettempfilename.asp

When the system shuts down, temporary files whose names have been created by
this function are not automatically deleted.


--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------
 
T

Tony Girgenti

Considering this, since i want the file to be available after the program
terminates, i don't think i should use this method to store my data. Even
though the file is not automatically deleted.

Do you agree ?

Thanks,
Tony

"Gaurav Vaish (www.EduJiniOnline.com)"
Does thae fact that GetTempFileName has the word "Temp" in it tell me
anything. Should i wonder if the file will remain after the program
terminates ?

Yes.
The folder in which it is created is always Path.GetTempPath
The name is randomly chosen so that it is unique.
The extension is always .tmp
The prefix is always tmp
The file is not temporary in the sense that it is not automatically
deleted.


The method is a wrapper around the native method GetTempFileName.
The details of the native method is available at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/gettempfilename.asp

When the system shuts down, temporary files whose names have been created
by this function are not automatically deleted.


--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

Considering this, since i want the file to be available after the program
terminates, i don't think i should use this method to store my data. Even
though the file is not automatically deleted.

Do you agree ?

Strictly speaking, all that is required is a valid, random, new name.
You have the following options:

1. Generate yourself
2. Ask somebody to generate
2a. Use GetTempFileName
2b. Directly invoke GetTempFileName since it can work with
non-GetTempPath folders as well.



--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top