如何在运行时创建文件夹

编程入门 行业动态 更新时间:2024-10-27 13:27:58
本文介绍了如何在运行时创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个登录表单,当用户登录时,我希望在运行时显示名为Images的文件夹。 当用户点击文件夹时,它会打开另一页(来自sql的数据)。 我使用以下代码创建了文件夹:

string NewDirectory = Server.MapPath( Images); CreateDirectoryIfNotExists(NewDirectory);

还创建了文件夹。如何在我的网页中显示文件夹?

解决方案

你可以使用简单的FolderViewer控件 [ ^ ]。

您可以列出您页面上的所有目录: 这是代码,

<%@ 导入 命名空间 = System.IO %> < html > < body > <% foreach ( var dir in new DirectoryInfo( E:\\MyFolder)。GetDirectories()){%> 目录:<% = dir.Name %> < br / > <% foreach ( var file in dir.GetFiles()){%> < % = file.Name %> < br / > <%} %> < br / > <%} %> < / body > < / html >

OR:

void GetDirectories( string path) { // 使用try catch,因为某些目录可能不允许您读取子文件夹,例如:您的antivruses Quarantine文件夹 尝试 { string [] directoryList = System。 IO.Directory.GetDirectories(路径); if (directoryList.Length > 0 ) { sb.Append( < ul>); foreach (字符串目录 in directoryList) { System.IO.FileAttributes f =( new System.IO.DirectoryInfo(directory))。属性; if ((f& System.IO.FileAttributes.Hidden)!= System.IO.FileAttributes.Hidden) { sb.Append( < li>)。Append(directory); GetDirectories(目录); sb.Append( < / li>); } } sb.Append( < / ul>); } } catch { } }

src:此处 [ ^ ]

亲爱的,只需将此代码放入页面加载事件即可。 这将创建一个名为MyFolder的文件夹。

string strpath = @ C:/ + MyFolder; // 检查是否存在同名目录的条件 if (!(Directory.Exists(strpath))) { Directory.CreateDirectory(strpath); lblMessage。 Text = 目录创建; } else { lblMessage。 Text = 目录已存在,名称相同; }

I have a login form, when user logins i want folder named Images to be displayed at run time. When user clicks folder it opens another page (datas from sql). I have created folder by using this code :

string NewDirectory = Server.MapPath("Images"); CreateDirectoryIfNotExists(NewDirectory);

Folder is also created. How to display the folder in my webpage?

解决方案

You can use A Simple FolderViewer Control[^].

You can list out all the directories on you page: here is the code,

<%@ Import Namespace="System.IO" %> <html> <body> <% foreach (var dir in new DirectoryInfo("E:\\MyFolder").GetDirectories()) { %> Directory: <%= dir.Name %><br /> <% foreach (var file in dir.GetFiles()) { %> <%= file.Name %><br /> <% } %> <br /> <% } %> </body> </html>

OR :

void GetDirectories(string path) { //use try catch since some directories may not allow you to read the subfolders eg:your antivruses Quarantine folder try { string[] directoryList = System.IO.Directory.GetDirectories(path); if (directoryList.Length > 0) { sb.Append("<ul>"); foreach (string directory in directoryList) { System.IO.FileAttributes f = (new System.IO.DirectoryInfo(directory)).Attributes; if ((f & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden) { sb.Append("<li>").Append(directory); GetDirectories(directory); sb.Append("</li>"); } } sb.Append("</ul>"); } } catch { } }

src : here[^]

Dear, Just Put this code in page load event. This will create a folder named MyFolder.

string strpath = @"C:/" + "MyFolder"; //Condition to check if any directory exists with same name if (!(Directory.Exists(strpath))) { Directory.CreateDirectory(strpath); lblMessage. Text = "Directory Created"; } else { lblMessage . Text = "Already Directory Exists with the same name"; }

更多推荐

如何在运行时创建文件夹

本文发布于:2023-11-13 09:17:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!