博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取文件目录的两种方法
阅读量:4129 次
发布时间:2019-05-25

本文共 3814 字,大约阅读时间需要 12 分钟。

1. 使用C#:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace StrTest

{
    class directory
    {
        public static void Main(string[] args)
        {
            string path = @"D:/English/";
            if (File.Exists(path))
            {
                // This path is a file
                ProcessFile(path);
                Console.ReadLine();
            }
            else if (Directory.Exists(path))
            {
                // This path is a directory
                ProcessDirectory(path,0);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }
        }

        // Process all files in the directory passed in, recurse on any directories
        // that are found, and process the files they contain.
        public static void ProcessDirectory(string targetDirectory, int i)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(targetDirectory);
            foreach (string fileName in fileEntries)
                ProcessFile(fileName);

            // Recurse into subdirectories of this directory.

            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            ArrayList al = new ArrayList();
            foreach (string subdirectory in subdirectoryEntries)
            {
                al.Add(subdirectory);
                Console.WriteLine(al);
                //i++;
                //ProcessDirectory(subdirectory, i);
            }
            Console.WriteLine(al);
        }

        // Insert logic for processing found files here.

        public static void ProcessFile(string path)
        {
            int i = path.LastIndexOf("//");
            string fileName;
            if (i >= 0)
            {
                string dirName = path.Substring(0,i);
                fileName = path.Substring(i+1).Trim(); //得到文件名
                Console.WriteLine(dirName);
                Console.WriteLine(fileName);
            }
        }
    }
}

2. 使用JavaScript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Forder</title>
<script language="javascript">

function getSubForderList(fldr)

{
    var fcFolder = new Enumerator(fldr.SubFolders); //子文件夹  
   
    var list = new Array;
       
    for (;   !fcFolder.atEnd();   fcFolder.moveNext())   //枚举所有子文件夹
    {
        list.push(fcFolder.item());
    }

    return list;

}

function bindSelect(sel,forder)
{
    var f = document.getElementById("path");
   
    if(forder == "null" || forder.IsRootFolder)
    {
        f.value = "";
        binDriver();
        return ;       
    }
   
    f.value = forder;
   
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fldr = fso.GetFolder(forder); 
   
    var list = getSubForderList(fldr);
   
    while(sel.options.length >0)
    {
        sel.options.remove(0);
    }
   
    var oOption ;
   
    oOption= document.createElement("OPTION");
    sel.options.add(oOption);
    oOption.innerText = "  刷新  ";
    oOption.value = forder;
   
    oOption= document.createElement("OPTION");
    sel.options.add(oOption);
    oOption.innerText = "//..";
    oOption.value = fldr.ParentFolder;
   
    for(var i=0;i<list.length;i++)
    {
        oOption = document.createElement("OPTION");
        sel.options.add(oOption);
        oOption.innerText = list[i].Name; //list[i].ShortName;
        oOption.value = list[i];
    }   
}

function getForder(sel)
{
    bindSelect(sel,sel.options[sel.selectedIndex].value);
}

</script>

<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</head>

<body>

<input id="path" width="260PX" size="28" />
<span class="STYLE1">cychai 搜集</span><br>
<select size="10" id="sel" style="width:200PX" οnchange="getForder(this)">
</select>

<script language="javascript">

function $(id)

{
     return (id.constructor == String) ? document.getElementById(id) : id;
}

function getDrives()

{
    var fso, a, n, e, x;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    e = new Enumerator(fso.Drives);
    a = new Array;
    for (; !e.atEnd(); e.moveNext())
    {
       x = e.item();
       a[a.length] = x.DriveLetter;
    }
    return(a);
}

function binDriver()

{
    var a = getDrives();
    var sel = $('sel');
       
    while(sel.options.length >0)
    {
        sel.options.remove(0);
    }
   
    for(var i=0; i<a.length; i++)
    {
         var op = document.createElement('OPTION');
         op.value = a[i] + ':';
         op.innerText   = a[i] + ':';
         sel.appendChild(op);
    }
}

binDriver();

</script>

</body>
</html>
 

转载地址:http://bmzvi.baihongyu.com/

你可能感兴趣的文章
组队总结
查看>>
Windows7下JDK+Tomcat的安装与配置
查看>>
缺省虚似主机 DefaultHost
查看>>
Tomcat 的 SSL 配置
查看>>
tomcat 默认项目设置
查看>>
Ubuntu 设置命令行分辨率正确方法
查看>>
Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
查看>>
JSTL标签 参考手册
查看>>
[Phonegap+Sencha Touch] 移动开发21 Sencha touch tapHold事件 触发时间太长的解决办法
查看>>
CXF学习笔记(1)-HelloWorld!-发布webservice .
查看>>
CXF学习笔记(2)-HelloWorld!-客户端调用 .
查看>>
CXF学习笔记(3)-HelloWorld!-通过servlet发布webservice .
查看>>
CXF学习笔记(4)-HelloWorld!-安全认证
查看>>
CXF WebService 1 准备工作
查看>>
CXF WebService 2 入门示例
查看>>
CXFWerService 3 对Interceptor拦截器的支持
查看>>
CXF WebService 4 传递复杂类型对象
查看>>
CXF WebService 5 整合Spring
查看>>
CXF Webservice 6 - Spring 3 整合Apache CXF WebService
查看>>
CXF WebService 7 - Spring整合CXF,发布RSETful 风格WebService
查看>>