1
我想在C#中创建一个Web服务器,我需要获取请求的URL,然后列出请求的文件和文件夹。这很好地获取第一个目录。C#Webserver请求的目录问题
例如,我的web服务器根目录是c:\ test当我打开本地主机时,我得到了测试文件夹的内容。说数据是C:\测试的子文件夹,我可以点击浏览器中的数据,然后进入C:\测试\数据,现在当我点击任何文件夹,然后获取请求带有%2F而不是c:\ test \ data \好吧,所以我卡住了。
代码以收到的请求:
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);
}
我们列出迪尔我有以下功能:
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);
}
任何帮助将高度赞赏。
谢谢
好小伙子,我得到了答案我自己的问题,但另一个问题出现了ieusing如下代码我在Windows应用程序的形式,但相同的代码工作得到错误罚款控制台应用程序。 Byte [] bReceive = new Byte [1024]; int i = mySocket.Receive(bReceive,bReceive.Length,0); //将字节转换为字符串 string sBuffer = Encoding.ASCII.GetString(bReceive); – tike 2010-03-03 20:48:46