Please convert short PHP Code to ASP.NET

<?php
$quotes = file("QSDatabase.php");
$quote = rand(0, sizeof($quotes)-1);
echo $quotes[$quote];
?>
LVL 6
dgelinasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tomvergoteCommented:
vb .NET or c#?
0
dgelinasAuthor Commented:
C#, sorry
0
tomvergoteCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

tomvergoteCommented:
you will need to add using statements like this too:
using System;
using System.IO;
using System.Collections;
0
dgelinasAuthor Commented:
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
dgelinasAuthor Commented:
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
dgelinasAuthor Commented:
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
tomvergoteCommented:
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
dgelinasAuthor Commented:
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
tomvergoteCommented:
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
tomvergoteCommented:
or
StreamReader objReader = new StreamReader(Request.PhysicalApplicationPath + "inside\misc\testimonials\quotes.txt");
in your case
0
dgelinasAuthor Commented:
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
tomvergoteCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgelinasAuthor Commented:
Great it works now :)

Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.