PDA

View Full Version : سوال: کار با Directory !



mohammad973
دوشنبه 01 مهر 1392, 09:30 صبح
سلام دوستان .
مقادیری که با Directory.GetDirectories(path); و Directory.GetFiles(path); بدست میاد رو ، چطور میشه توی صفحه وبمون نشون بدیم ؟ مثلا به یه کنترل asp.net وصلش کنیم تا نمایش داده بشه ؟

ممنون میشم پاسخ بدید ... :متفکر:

مهدی رحیم زاده
دوشنبه 01 مهر 1392, 11:01 صبح
با سلام
راستش بنده هم یک چیزی شبیه به این رو باید طراحی کنم. من خودم با جی کوئری میخوام این رو تولید کنم. کار ساده ایه قبلا روش کار کردم.توی application میشد از listview استفاده کرد، توی وب نمیدونم که هست listview یا نه.
موفق و پیروز باشید

mehrzad_ali
دوشنبه 01 مهر 1392, 11:36 صبح
سلام این کد فولدر ها و فایل های مسیر رو میریزه توی treeview


public void getSubDirectories(string path, TreeNode node)
{
//---get sub directories under the current one
string[] dirs = Directory.GetDirectories(path);

//---no subdirectories
if (dirs.Length == 0)
{
return;
}
else
{
//---for each subdirectory, add to TreeView and
// recursively call itself again
string dir = null;
foreach (string dir_loopVariable in dirs)
{
dir = dir_loopVariable;
//---Add the relatve path to the TreeView control
TreeNode newNode = new TreeNode(dir.Substring(dir.LastIndexOf("\\") + 1));
newNode.ToolTip = dir;
node.ChildNodes.Add(newNode);
//---Find its subdirectories
getSubDirectories(dir, newNode);
//---Find files under itself
getFiles(dir, newNode);
//---close the current node
newNode.CollapseAll();
}
}
}

public void getFiles(string path, TreeNode node)
{
string[] files = Directory.GetFiles(path);
string file = null;

//---no files in directory and no subdirectory
if (files.Length == 0 & node.ChildNodes.Count == 0)
{
TreeNode newNode = new TreeNode("Directory is empty");
node.ChildNodes.Add(newNode);
return;
}
foreach (string file_loopVariable in files)
{
file = file_loopVariable;
//---Add the file to the TreeView control
TreeNode newNode = new TreeNode(file.Substring(path.Length + 1));
newNode.ToolTip = file;
newNode.ImageUrl = "Images\\file.gif";
node.ChildNodes.Add(newNode);
}
}

public void Tree_Show(TreeNode Father, string Url)
{

string[] get = Directory.GetDirectories(Url);

foreach (string s in get)
{
int p = s.LastIndexOf("\\");
TreeNode n = new TreeNode();
n.Text = s.Substring(p + 1);
n.Value = s;
TreeView1.Nodes.Add(n);
Tree_Show(n, s);
}
}

اینجا هم صدا زده میشه تابع



protected void Page_Load(object sender, EventArgs e)
{

if (TreeView1.SelectedNode != null)
{
Response.Write(TreeView1.SelectedNode.Text);
}
TreeView1.Nodes.Add(new TreeNode(Request.PhysicalApplicationPath));
getSubDirectories(Request.PhysicalApplicationPath, TreeView1.Nodes[0]);

}

mohammad973
دوشنبه 01 مهر 1392, 11:38 صبح
سلام . من خودم از این استفاده کردم . شاید برای دوستان مفید باشه ::




using System.IO;
using System.Text;


DirectoryInfo dir;
StringBuilder sb = new StringBuilder();
FileInfo[] files;

dir = new DirectoryInfo(Server.MapPath("."));
files = dir.GetFiles();
foreach (FileInfo f in files)
{
sb.Append("<a href=\"" + f.Name.ToString() + "\">");
sb.Append(f.Name.ToString() + "</a><br />");
}
Literal1.Text = sb.ToString();

barnamenevisforme
سه شنبه 02 مهر 1392, 03:29 صبح
سلام دوست عزیز
از این روش میشه برای اصلاح آدرس ،و جلوگیری از خطاهای گشت و گذار در سایت(بعد از اپلود) استفاده کرد؟

amin750
یک شنبه 27 مهر 1393, 12:53 عصر
سلام دوستان
من به یه مشکلی برخوردم اگه ممکنه کمکم کنین
من با این کد میخوام لیست دایرکتوری هایی رو که توی مسیر زیر هستش رو بدست بیارم ولی نمیشه و فقط یکی از سه تا پوشه موجود به نشون میده
String[] directory = Directory.GetDirectories(@"~/images/gallery/");
ListBox1.DataSource = directory;
ListBox1.DataBind();

یه لیست باکس هم گذاشتم تا لیست پوشه ها رو نشون بده ولی فقط یکی از 3 پوشه رو نشون میده اونم دومی رو
لطفا راهنمایی کنین
ممنون