开发者

C# Webserver requested directory issue

I am trying to make a web server in C#, i need to get the requested url and then list the files and folders requested.This is good to get the first directory .

For Eg. My webserver root is c:\test when i open localhost i get the contents of test folder. Say Data is subfolder of c:\test, i can click on data from the browser and get into C:\test\data now when i click on开发者_运维百科 any folder then the get request comes with %2F instead of c:\test\data\ok and so i am stuck.

Code to Recieve The Request :

 sRequest = sBuffer.Substring(0, iStartPos - 1);
    sRequest.Replace("\\", "/");
 if ((sRequest.IndexOf(".") < 1) && (!sRequest.EndsWith("/")))
                    {
                        sRequest = sRequest + "/";

                    }
iStartPos = sRequest.LastIndexOf("/") + 1;
                    sRequestedFile = sRequest.Substring(iStartPos);
sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/") - 3);

if (sDirName == "/")
                        sLocalDir = sMyWebServerRoot;
                    else
                    {
                        //Get the Virtual Directory
                        // sLocalDir = GetLocalPath(sMyWebServerRoot, sDirName);
                        Console.WriteLine("i am here");
                        sDirName = sDirName.Substring(1, sDirName.Length - 2);
                        //sDirName = sDirName.Replace("/", "\\");
                        Console.WriteLine("Amit:" + sDirName);
                        string test1 = Path.Combine("C:\\test\\", sDirName);
                        sLocalDir = Path.Combine(@"C:\\test", sDirName);
                    }

Now to List Dir I have the following Function :

public String listdir(string sLocaldir,string sDirName)
        {
            string sresult = "";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<html>");
            sb.AppendLine("<head>");
            sb.AppendLine("<title>Test</title>");
            sb.AppendLine("</head>");
            sb.AppendLine("<body>");
            sb.AppendLine("<h1><center><u>Listing Folders Under " + sLocaldir + "</h1></u></center>");
            string[] folderpaths = Directory.GetDirectories(sLocaldir);

            sb.AppendLine("<font color=red><font size=5>Listing Directories<br>");
            for (int i = 0; i < folderpaths.Length; i++)
            {
                string fpath = folderpaths[i];
                string[] foldernames = fpath.Split('\\');
                int j = foldernames.Length - 1;
                string fname = foldernames[j];
                string fullname;
                if (sDirName != "/")
                {
                  //fname= fname + "\\";
                  fullname = sDirName +"/"+ fname;
                  //fullname = fullname.Replace("\\", "/");
                    //fullname = Path.GetPathRoot("C:\\test");
                    Console.WriteLine("Get full path:" + fullname);
                }
                else
                {
                    fullname = fname;
                }
                Console.WriteLine("Full Test:" + fullname);
                //sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                    HttpUtility.HtmlEncode(HttpUtility.UrlEncode(fullname )),
                    HttpUtility.HtmlEncode(fname)));
            }
            string[] filePaths = Directory.GetFiles(@"C:\test");
            sb.AppendLine("<font color=red><font size=5>Listing Files<br>");
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string name = Path.GetFileName(filePaths[i]);

                sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                    HttpUtility.HtmlEncode(HttpUtility.UrlEncode(name)),
                    HttpUtility.HtmlEncode(name)));
            }

            sb.AppendLine("</ul>");
            sb.AppendLine("</body>");
            sb.AppendLine("</html>");
            sresult = sb.ToString();
            return sresult;
            //Console.WriteLine(sresult);
        }

Any help would be highly appreciated.

Thank you


%2F is safe encoding for the / symbol. You are HTMLEncoding the / symbol in your code above.

Your approach can be much simpler see:

http://www.codeproject.com/KB/IP/mywebserver.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜