[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Please convert short PHP Code to ASP.NET

Posted on 2004-11-23
14
Medium Priority
?
232 Views
Last Modified: 2006-11-17
<?php
$quotes = file("QSDatabase.php");
$quote = rand(0, sizeof($quotes)-1);
echo $quotes[$quote];
?>
0
Comment
Question by:dgelinas
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:tomvergote
ID: 12655302
vb .NET or c#?
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12655315
C#, sorry
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12655508
try this

                  // create a reader for the file
                  StreamReader objReader = new StreamReader("list.txt");
                  ArrayList quotes = new ArrayList();
                  string sLine;
                  // fill an array with lines
                  do
                  {
                        sLine = objReader.ReadLine();
                        quotes.Add(sLine);
                        
                  } while (sLine != null);
                  objReader.Close();
                  // get number of lines
                  int numlines = quotes.Count;
                  // get random line number
                  Random rnd = new Random();
                  int quote = rnd.Next(0,numlines -1);
                  // get random quote
                  string rndquote = (string)quotes[quote];
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:tomvergote
ID: 12655521
you will need to add using statements like this too:
using System;
using System.IO;
using System.Collections;
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12655804
Getting this error

CS1519: Invalid token 'using' in class, struct, or interface member declaration

for these guys

using System;
using System.IO;
using System.Collections;
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12655823
Code looks like this

<script language="c#" runat="server">
               using System;
               using System.IO;
               using System.Collections;
               // create a reader for the file
               StreamReader objReader = new StreamReader("../inside/misc/testimonials/quotes.txt");
               ArrayList quotes = new ArrayList();
               string sLine;
               // fill an array with lines
               do
               {
                    sLine = objReader.ReadLine();
                    quotes.Add(sLine);
                   
               } while (sLine != null);
               objReader.Close();
               // get number of lines
               int numlines = quotes.Count;
               // get random line number
               Random rnd = new Random();
               int quote = rnd.Next(0,numlines -1);
               // get random quote
               string rndquote = (string)quotes[quote];       
                  
   </script>
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12655904
Ok I think I'm a little further now.

How do I get this to be a relative path or virtual path

StreamReader objReader = new StreamReader("../inside/misc/testimonials/quotes.txt");
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12655938
you can add relative paths in asp.NET like this:
~/path/to/file where ~ represents your IIS virtual root
or
../path where you start from your bin I think, would have to test that ou
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12655965
this

               StreamReader objReader = new StreamReader("~/inside/misc/testimonials/quotes.txt");

gives me

               Could not find a part of the path 'C:\WINNT\system32\~\inside\misc\testimonials\quotes.txt'.
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12656077
hmm, that didn't work as I expected apparently
you can use Request.PhysicalApplicationPath to get at the base path and try something like this as a quick workaround
StreamReader objReader = new StreamReader(Request.PhysicalApplicationPath + "list.txt");
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12656091
or
StreamReader objReader = new StreamReader(Request.PhysicalApplicationPath + "inside\misc\testimonials\quotes.txt");
in your case
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12656118
Compiler Error Message: CS1009: Unrecognized escape sequence

Source Error:


Line 105:<%
Line 106:                     // create a reader for the file
Line 107:               StreamReader objReader = new StreamReader(Request.PhysicalApplicationPath
 "inside\misc\testimonials\quotes.txt");
Line 108:               ArrayList quotes = new ArrayList();
Line 109:               string sLine;
 
0
 
LVL 8

Accepted Solution

by:
tomvergote earned 400 total points
ID: 12656169
euh, my bad, you'll have to escape backslashes (copy pasted in firefox without testing in v studio)
StreamReader objReader = new StreamReader(Request.PhysicalApplicationPath + "inside\\misc\\testimonials\\quotes.txt");
0
 
LVL 6

Author Comment

by:dgelinas
ID: 12656219
Great it works now :)

Thanks for the help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question