Read file once

X

xplode144

I have a Web application. i need to read a file once during the startup and
preserve the read data throughout the life of the application. i will to
access the data often during the page_load of the default.aspx page.

what is the best way to do so? do i need to implement a singlton object?
 
E

ed

Hi xplode144,

[This post assumes that by "Application" you're talking about the same
kinda thing that ASP.NET calls an "Application".]

For application-wide data from a file, you've already got web.config.
Use that if you can because it's easy and it's at least as fast as the
approach you're proposing.

That said, here's how to do what you asked for (broad stokes only):

Find the Application_Start event in Global.asax and put the read-file
code there. Save any values you need in the Application object:

// C#
Application["xplode144"] = "abcdef";
Application["otherfilevalue"] = 123
.. . . and so on

You'll have to restart IIS to get new values into your application
(unless you add events to watch for file changes, but that just gets
you in deeper).

Alternatives:
- web.config
- Caching (big topic but there are a lot of how-to's; it may not be
relevant to what you're trying to do)
- Just read the file each time; it doesn't take that long


You don't need a Singleton for any of the above.

HTH,
Ed
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top