Read file once

  • Thread starter Thread starter xplode144
  • Start date Start date
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?
 
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
 
Back
Top