2

i wanted to create a web server while on the process.....

i am not being able to create a dynamic html which could take link from my c# console application...

for example i have a code which shows files from the system.. for example "c:\tike\a.jpeg" now i wanted to make that particular link a a href link in my html page...

any help would be appreciated..... thank you..

(to sum up ... i want to create dynamic html page which takes value from c# console application.)

3
  • To clarify: From your previous questions it seems you're building a web-server. Now you want to generate an "index" page for a directory, i.e. an HTML page that automatically lists all files in that directory with HTML links to the files. Is this correct? Commented Jan 30, 2010 at 0:15
  • @dtb yes.. you are rite. Commented Jan 30, 2010 at 0:22
  • @james Black.. i am blank in this context so i wanted to get some idea if possible few hint on code would be great.... thanks. Commented Jan 30, 2010 at 0:23

1 Answer 1

6

Ignoring virtual paths etc. for now, here is a simple example to get you started:

StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<head>");
sb.AppendLine("<title>Index of c:\\dir</title>");
sb.AppendLine("</head>");
sb.AppendLine("<body>");
sb.AppendLine("<ul>");

string[] filePaths = Directory.GetFiles(@"c:\dir");
for (int i = 0; i < filePaths.Length; ++i) {
    string name = Path.GetFileName(filePaths[i]);

    sb.AppendLine(string.Format("<li><a href=\"{0}\">{1}</a></li>",
        HttpUtility.HtmlEncode(HttpUtility.UrlEncode(name)),
        HttpUtility.HtmlEncode(name)));
}

sb.AppendLine("</ul>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");
string result = sb.ToString();

result contains a string that you can send as body of an HTTP response to the web-browser.

(Note: I typed the code right into the answer box, no idea if it compiles as-is.)

Sign up to request clarification or add additional context in comments.

2 Comments

No overload for method 'Appendline' takes '3' arguments
Forgot string.Format. Fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.