获取文件目录的两种方法
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
获取文件目录的两种方法
最新推荐文章于 2020-11-22 08:39:44 发布