本文介绍了如何在运行时创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个登录表单,当用户登录时,我希望在运行时显示名为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"; }更多推荐
如何在运行时创建文件夹
发布评论