asp.net(C#)读取服务器根目录下的文件列表

时间:2022-01-03 12:30:54
我一点都不懂asp.net,希望高手们能贴出一个在环境中一直可以运行实现的代码.

谢谢.

4 个解决方案

#1


  string dirPath= HttpContext.Current.Server.MapPath("../book/");
             if (Directory.Exists(dirPath))
            {
                //获得目录信息
                DirectoryInfo dir = new DirectoryInfo(dirPath);
                //获得目录文件列表
                FileInfo[] files = dir.GetFiles("*.*");
                string[] fileNames = new string[files.Length];
                int i = 0;
                foreach (FileInfo fileInfo in files)
                {
                    fileNames[i] = fileInfo.Name;
                    i++;
                }
                return fileNames;
            }

#2


能给的详细点吗?全部代码,我是准备放在1.aspx里的,是在浏览器里面看的那种

谢谢

在线等,分一定加

#3



private string[] ReturnFilesArray() {
if (CurrentImagesFolder.Value != "") {
try {
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string ImageFolderPath = AppPath + CurrentImagesFolder.Value;
string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*");
return FilesArray;


} catch {

return null;
}
} else {
return null;
}

}

private string[] ReturnDirectoriesArray() {
if (CurrentImagesFolder.Value != "") {
try {
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string CurrentFolderPath = AppPath + CurrentImagesFolder.Value;
string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*");
return DirectoriesArray ;
} catch {
return null;
}
} else {
return null;
}
}

public void DisplayImages() {
string[] FilesArray = ReturnFilesArray();
string[] DirectoriesArray = ReturnDirectoriesArray();
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string AppUrl;

//Get the application's URL
if (Request.ApplicationPath == "/")
AppUrl = Request.ApplicationPath;
else
AppUrl = Request.ApplicationPath + "/";

GalleryPanel.Controls.Clear();
if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {
gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value;
} else {
string ImageFileName = "";
string ImageFileLocation = "";

int thumbWidth = 94;
int thumbHeight = 94;

if (CurrentImagesFolder.Value != RootImagesFolder.Value) {

System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = AppUrl + "images/ftb/folder.up.gif";
myHtmlImage.Attributes["unselectable"]="on"; 
myHtmlImage.Attributes["align"]="absmiddle"; 
myHtmlImage.Attributes["vspace"]="36"; 

string ParentFolder = CurrentImagesFolder.Value.Substring(0,CurrentImagesFolder.Value.LastIndexOf("\\"));

System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["unselectable"]="on"; 
myImageHolder.Attributes["onclick"]="divClick(this,'');";  
myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + ParentFolder.Replace("\\","\\\\") + "');";  
myImageHolder.Controls.Add(myHtmlImage);

System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl("向上"));
myMainHolder.Controls.Add(myTitleHolder);

GalleryPanel.Controls.Add(myMainHolder);

}

foreach (string _Directory in DirectoriesArray) {

try {
string DirectoryName = _Directory.ToString();


System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = AppUrl + "images/ftb/folder.big.gif";
myHtmlImage.Attributes["unselectable"]="on"; 
myHtmlImage.Attributes["align"]="absmiddle"; 
myHtmlImage.Attributes["vspace"]="29"; 

System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["unselectable"]="on"; 
myImageHolder.Attributes["onclick"]="divClick(this);";  
myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + DirectoryName.Replace(AppPath,"").Replace("\\","\\\\") + "');";  
myImageHolder.Controls.Add(myHtmlImage);

System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl(DirectoryName.Replace(AppPath + CurrentImagesFolder.Value + "\\","")));
myMainHolder.Controls.Add(myTitleHolder);

GalleryPanel.Controls.Add(myMainHolder);
} catch {
// nothing for error
}
}

foreach (string ImageFile in FilesArray) {

try {

ImageFileName = ImageFile.ToString();
ImageFileName = ImageFileName.Substring(ImageFileName.LastIndexOf("\\")+1);
ImageFileLocation = AppUrl;
ImageFileLocation = ImageFileLocation.Substring(ImageFileLocation.LastIndexOf("\\")+1);
//galleryfilelocation += "/";
ImageFileLocation += CurrentImagesFolder.Value;
ImageFileLocation += "/";
ImageFileLocation += ImageFileName;
System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = ImageFileLocation;
System.Drawing.Image myImage = System.Drawing.Image.FromFile(ImageFile.ToString());
myHtmlImage.Attributes["unselectable"]="on";  
//myHtmlImage.border=0;

// landscape image
if (myImage.Width > myImage.Height) {
if (myImage.Width > thumbWidth) {
myHtmlImage.Width = thumbWidth;
myHtmlImage.Height = Convert.ToInt32(myImage.Height * thumbWidth/myImage.Width);
} else {
myHtmlImage.Width = myImage.Width;
myHtmlImage.Height = myImage.Height;
}
// portrait image
} else {
if (myImage.Height > thumbHeight) {
myHtmlImage.Height = thumbHeight;
myHtmlImage.Width = Convert.ToInt32(myImage.Width * thumbHeight/myImage.Height);
} else {
myHtmlImage.Width = myImage.Width;
myHtmlImage.Height = myImage.Height;
}
}

if (myHtmlImage.Height < thumbHeight) {
myHtmlImage.Attributes["vspace"] = Convert.ToInt32((thumbHeight/2)-(myHtmlImage.Height/2)).ToString(); 
}


System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["onclick"]="divClick(this,'" + ImageFileName + "');";  
myImageHolder.Attributes["ondblclick"]="returnImage('" + ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";  
myImageHolder.Controls.Add(myHtmlImage);


System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl(ImageFileName + "<BR>" + myImage.Width.ToString() + "x" + myImage.Height.ToString()));
myMainHolder.Controls.Add(myTitleHolder);

//GalleryPanel.Controls.Add(myImage);
GalleryPanel.Controls.Add(myMainHolder);

myImage.Dispose();
} catch {

}
}
gallerymessage.Text = "";
}
}

#4


#1 的方法很简洁嘛!! 我试一下!

string dirPath= HttpContext.Current.Server.MapPath("../book/");
  if (Directory.Exists(dirPath))
  {
  //获得目录信息
  DirectoryInfo dir = new DirectoryInfo(dirPath);
  //获得目录文件列表
  FileInfo[] files = dir.GetFiles("*.*");
  string[] fileNames = new string[files.Length];
  int i = 0;
  foreach (FileInfo fileInfo in files)
  {
  fileNames[i] = fileInfo.Name;
  i++;
  }
  return fileNames;
  }

#1


  string dirPath= HttpContext.Current.Server.MapPath("../book/");
             if (Directory.Exists(dirPath))
            {
                //获得目录信息
                DirectoryInfo dir = new DirectoryInfo(dirPath);
                //获得目录文件列表
                FileInfo[] files = dir.GetFiles("*.*");
                string[] fileNames = new string[files.Length];
                int i = 0;
                foreach (FileInfo fileInfo in files)
                {
                    fileNames[i] = fileInfo.Name;
                    i++;
                }
                return fileNames;
            }

#2


能给的详细点吗?全部代码,我是准备放在1.aspx里的,是在浏览器里面看的那种

谢谢

在线等,分一定加

#3



private string[] ReturnFilesArray() {
if (CurrentImagesFolder.Value != "") {
try {
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string ImageFolderPath = AppPath + CurrentImagesFolder.Value;
string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*");
return FilesArray;


} catch {

return null;
}
} else {
return null;
}

}

private string[] ReturnDirectoriesArray() {
if (CurrentImagesFolder.Value != "") {
try {
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string CurrentFolderPath = AppPath + CurrentImagesFolder.Value;
string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*");
return DirectoriesArray ;
} catch {
return null;
}
} else {
return null;
}
}

public void DisplayImages() {
string[] FilesArray = ReturnFilesArray();
string[] DirectoriesArray = ReturnDirectoriesArray();
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
string AppUrl;

//Get the application's URL
if (Request.ApplicationPath == "/")
AppUrl = Request.ApplicationPath;
else
AppUrl = Request.ApplicationPath + "/";

GalleryPanel.Controls.Clear();
if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {
gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value;
} else {
string ImageFileName = "";
string ImageFileLocation = "";

int thumbWidth = 94;
int thumbHeight = 94;

if (CurrentImagesFolder.Value != RootImagesFolder.Value) {

System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = AppUrl + "images/ftb/folder.up.gif";
myHtmlImage.Attributes["unselectable"]="on"; 
myHtmlImage.Attributes["align"]="absmiddle"; 
myHtmlImage.Attributes["vspace"]="36"; 

string ParentFolder = CurrentImagesFolder.Value.Substring(0,CurrentImagesFolder.Value.LastIndexOf("\\"));

System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["unselectable"]="on"; 
myImageHolder.Attributes["onclick"]="divClick(this,'');";  
myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + ParentFolder.Replace("\\","\\\\") + "');";  
myImageHolder.Controls.Add(myHtmlImage);

System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl("向上"));
myMainHolder.Controls.Add(myTitleHolder);

GalleryPanel.Controls.Add(myMainHolder);

}

foreach (string _Directory in DirectoriesArray) {

try {
string DirectoryName = _Directory.ToString();


System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = AppUrl + "images/ftb/folder.big.gif";
myHtmlImage.Attributes["unselectable"]="on"; 
myHtmlImage.Attributes["align"]="absmiddle"; 
myHtmlImage.Attributes["vspace"]="29"; 

System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["unselectable"]="on"; 
myImageHolder.Attributes["onclick"]="divClick(this);";  
myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + DirectoryName.Replace(AppPath,"").Replace("\\","\\\\") + "');";  
myImageHolder.Controls.Add(myHtmlImage);

System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl(DirectoryName.Replace(AppPath + CurrentImagesFolder.Value + "\\","")));
myMainHolder.Controls.Add(myTitleHolder);

GalleryPanel.Controls.Add(myMainHolder);
} catch {
// nothing for error
}
}

foreach (string ImageFile in FilesArray) {

try {

ImageFileName = ImageFile.ToString();
ImageFileName = ImageFileName.Substring(ImageFileName.LastIndexOf("\\")+1);
ImageFileLocation = AppUrl;
ImageFileLocation = ImageFileLocation.Substring(ImageFileLocation.LastIndexOf("\\")+1);
//galleryfilelocation += "/";
ImageFileLocation += CurrentImagesFolder.Value;
ImageFileLocation += "/";
ImageFileLocation += ImageFileName;
System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
myHtmlImage.Src = ImageFileLocation;
System.Drawing.Image myImage = System.Drawing.Image.FromFile(ImageFile.ToString());
myHtmlImage.Attributes["unselectable"]="on";  
//myHtmlImage.border=0;

// landscape image
if (myImage.Width > myImage.Height) {
if (myImage.Width > thumbWidth) {
myHtmlImage.Width = thumbWidth;
myHtmlImage.Height = Convert.ToInt32(myImage.Height * thumbWidth/myImage.Width);
} else {
myHtmlImage.Width = myImage.Width;
myHtmlImage.Height = myImage.Height;
}
// portrait image
} else {
if (myImage.Height > thumbHeight) {
myHtmlImage.Height = thumbHeight;
myHtmlImage.Width = Convert.ToInt32(myImage.Width * thumbHeight/myImage.Height);
} else {
myHtmlImage.Width = myImage.Width;
myHtmlImage.Height = myImage.Height;
}
}

if (myHtmlImage.Height < thumbHeight) {
myHtmlImage.Attributes["vspace"] = Convert.ToInt32((thumbHeight/2)-(myHtmlImage.Height/2)).ToString(); 
}


System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
myImageHolder.CssClass = "imageholder";
myImageHolder.Attributes["onclick"]="divClick(this,'" + ImageFileName + "');";  
myImageHolder.Attributes["ondblclick"]="returnImage('" + ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";  
myImageHolder.Controls.Add(myHtmlImage);


System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
myMainHolder.CssClass = "imagespacer";
myMainHolder.Controls.Add(myImageHolder);

System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
myTitleHolder.CssClass = "titleHolder";
myTitleHolder.Controls.Add(new LiteralControl(ImageFileName + "<BR>" + myImage.Width.ToString() + "x" + myImage.Height.ToString()));
myMainHolder.Controls.Add(myTitleHolder);

//GalleryPanel.Controls.Add(myImage);
GalleryPanel.Controls.Add(myMainHolder);

myImage.Dispose();
} catch {

}
}
gallerymessage.Text = "";
}
}

#4


#1 的方法很简洁嘛!! 我试一下!

string dirPath= HttpContext.Current.Server.MapPath("../book/");
  if (Directory.Exists(dirPath))
  {
  //获得目录信息
  DirectoryInfo dir = new DirectoryInfo(dirPath);
  //获得目录文件列表
  FileInfo[] files = dir.GetFiles("*.*");
  string[] fileNames = new string[files.Length];
  int i = 0;
  foreach (FileInfo fileInfo in files)
  {
  fileNames[i] = fileInfo.Name;
  i++;
  }
  return fileNames;
  }