如何从ASP.NET中的Web服务器搜索文件 [英] How to search a file from a Web server in ASP.NET
问题描述
大家好
我需要在托管我的网站的服务器中搜索pdf,doc,jpg等文件.我现在正在开发一个将在Intranet中托管和使用的网站.我现在在文件系统中工作.
我的实际问题是我不知道如何通过程序表示服务器,即不知道如何设置将文件上传到服务器的路径.
可以在上面的代码上帮助我吗..
我尝试了以下代码:-
hi all
i need to search files like, pdf,doc,jpg etc in a server where my site is hosted. i am now developing a website that is to be hosted and used in intranet. i am now working in file system.
my actual problem is i don''t know how to represent the server through program , that is, don''t know how to set the path to upload the files to server.
can any on help me with code for the above..
i tried the following code:-
protected void Button1_Click(object sender, EventArgs e)
{
string filename = TextBox1.Text;
if (File.Exists(Server.MapPath("testit" + ( filename))))
// if (File.Exists(Server.MapPath(filename)))
{
Response.Write("file exist");
}
else
{
Response.Write("no");
}
}
testit是我用来在IIS中托管我的网站的虚拟目录
在此先感谢.....
我上一个问题中的解决方案无法正常工作,请任何人帮助我
testit is the virtual directory that i made for hosting my website in IIS
thanks in advance.....
Solution in my previous question is not working any one plz help me
推荐答案
//string filename = TextBox1.Text;
if (File.Exists(Server.MapPath("Image/untitled.png")))
// if (File.Exists(Server.MapPath(filename)))
Response.Write("file exist");
else
{
Response.Write("no");
}
这里"Image"是解决方案中的文件夹,"untitled.png"是此文件夹中的文件,我能够得到它.我认为在文本框中您没有获得正确的文件路径.
here "Image" is folder in solution and "untitled.png" is file in this folder and i am able to get this. I think in text box you are not getting correct file path.
您可以尝试以下代码在目录中搜索文件:
You can try this code to search files in the directory:
public static void disp(string source)
{
DirectoryInfo dr = new DirectoryInfo(source);
DirectoryInfo dir = new DirectoryInfo(des);
foreach (FileInfo fs in dr.GetFiles())
{
path = fs.Extension;
switch (path)
{
case ".doc":
if (!Directory.Exists("DOC"))
{
dir.CreateSubdirectory("DOC");
}
Console.WriteLine("file" + fs.Name);
break;
case ".txt":
if (!Directory.Exists("txt"))
{
dir.CreateSubdirectory("txt");
}
Console.WriteLine("file" + fs.Name);
break;
case ".pdf":
if (!Directory.Exists("txt"))
{
dir.CreateSubdirectory("txt");
}
Console.WriteLine("file" + fs.Name);
break;
}
}
}
这篇关于如何从ASP.NET中的Web服务器搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!