??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
StreamWriter sw = File.AppendText(Server.MapPath(??+?a href="file://myText.txt/">\\myText.txt?;
sw.WriteLine(”追逐理想?;
sw.WriteLine(”kzlll?;
sw.WriteLine(?NETW记?;
sw.Flush();
sw.Close();
C#拯文g
string OrignFile,NewFile;
OrignFile = Server.MapPath(??+?a href="file://myText.txt/">\\myText.txt?
NewFile = Server.MapPath(??+?a href="file://myTextCopy.txt/">\\myTextCopy.txt?
File.Copy(OrignFile,NewFile,true);
C#删除文g
string delFile = Server.MapPath(??+?a href="file://myTextCopy.txt/">\\myTextCopy.txt?
File.Delete(delFile);
C#Ud文g
string OrignFile,NewFile;
OrignFile = Server.MapPath(??+?a href="file://myText.txt/">\\myText.txt?
NewFile = Server.MapPath(??+?a href="file://myTextCopy.txt/">\\myTextCopy.txt?
File.Move(OrignFile,NewFile);
C#创徏目录
// 创徏目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory(”c:\\sixAge?;
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory(”sixAge1?;
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory(”sixAge1_1?;
// 当前目录设为c:\sixAge
Directory.SetCurrentDirectory(”c:\\sixAge?;
// 创徏目录c:\sixAge\sixAge2
Directory.CreateDirectory(”sixAge2?;
// 创徏目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory(”sixAge2\\sixAge2_1?;
递归删除文g夹及文g
<%@ Page Language=C#%>
<%@ Import namespace=”System.IO?>
<Script runat=server>
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在q个文g夹删除之
{
foreach(string d in Directory.GetFileSystemEntries(dir))
{
if(File.Exists(d))
File.Delete(d); //直接删除其中的文?br /> else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文g?br /> Response.Write(dir+?文g夹删除成功?;
}
else
Response.Write(dir+?该文件夹不存在?; //如果文g夹不存在则提C?br />}
protected void Page_Load (Object sender ,EventArgs e)
{
string Dir=”D:\\gbook\\11?
DeleteFolder(Dir); //调用函数删除文g?br />}
// ======================================================
// 实现一个静态方法将指定文g夹下面的所有内容copy到目标文件夹下面
// 如果目标文g夹ؓ只读属性就会报错?br /> // April 18April2005 In STU
// ======================================================
public static void CopyDir(string srcPath,string aimPath)
{
try
{
// 查目标目录是否以目录分割字符l束如果不是则添加之
if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新徏?br /> if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
// 得到源目录的文g列表Q该里面是包含文件以及目录\径的一个数l?br /> // 如果你指向copy目标文g下面的文件而不包含目录请用下面的Ҏ
// string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文g和目?br /> foreach(string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文g
if(Directory.Exists(file))
CopyDir(file,aimPath+Path.GetFileName(file));
// 否则直接Copy文g
else
File.Copy(file,aimPath+Path.GetFileName(file),true);
}
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
}
// ======================================================
// 实现一个静态方法将指定文g夹下面的所有内容Detele
// 试的时候要心操作Q删除之后无法恢复?br /> // April 18April2005 In STU
// ======================================================
public static void DeleteDir(string aimPath)
{
try
{
// 查目标目录是否以目录分割字符l束如果不是则添加之
if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 得到源目录的文g列表Q该里面是包含文件以及目录\径的一个数l?br /> // 如果你指向Delete目标文g下面的文件而不包含目录请用下面的Ҏ
// string[] fileList = Directory.GetFiles(aimPath);
string[] fileList = Directory.GetFileSystemEntries(aimPath);
// 遍历所有的文g和目?br /> foreach(string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Delete该目录下面的文g
if(Directory.Exists(file))
{
DeleteDir(aimPath+Path.GetFileName(file));
}
// 否则直接Delete文g
else
{
File.Delete (aimPath+Path.GetFileName(file));
}
}
//删除文g?br /> System.IO .Directory .Delete (aimPath,true);
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
}
需要引用命名空_
using System.IO;
/**//// <summary>
/// 拯文g?包括子文件夹)到指定文件夹?源文件夹和目标文件夹均需l对路径. 格式: CopyFolder(源文件夹,目标文g?;
/// </summary>
/// <param name=”strFromPath?gt;</param>
/// <param name=”strToPath?gt;</param>
//————————————————?br /> //作?明天去要饭?QQ:305725744
//————————————————?/p>
public static void CopyFolder(string strFromPath,string strToPath)
{
//如果源文件夹不存在,则创?br /> if (!Directory.Exists(strFromPath))
{
Directory.CreateDirectory(strFromPath);
}
//取得要拷贝的文g夹名
string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf(”\\? + 1,strFromPath.Length - strFromPath.LastIndexOf(”\\? - 1);
//如果目标文g夹中没有源文件夹则在目标文g夹中创徏源文件夹
if (!Directory.Exists(strToPath + “\\?+ strFolderName))
{
Directory.CreateDirectory(strToPath + “\\?+ strFolderName);
}
//创徏数组保存源文件夹下的文g?br /> string[] strFiles = Directory.GetFiles(strFromPath);
//循环拯文g
for(int i = 0;i < strFiles.Length;i++)
{
//取得拯的文件名Q只取文件名Q地址截掉?br /> string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf(”\\? + 1,strFiles[i].Length - strFiles[i].LastIndexOf(”\\? - 1);
//开始拷贝文?true表示覆盖同名文g
File.Copy(strFiles[i],strToPath + “\\?+ strFolderName + “\\?+ strFileName,true);
}
//创徏DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文g夹名U?br /> DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0;j < ZiPath.Length;j++)
{
//获取所有子文g夹名
string strZiPath = strFromPath + “\\?+ ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹Q从头开始新一轮的拯
CopyFolder(strZiPath,strToPath + “\\?+ strFolderName);
}
}
一,哈希?Hashtable)q?/font>
?NET Framework中,Hashtable是System.Collections命名I间提供的一个容器,用于处理和表现类似keyvalue的键值对Q其中key通常可用来快速查找,同时key是区分大写Qvalue用于存储对应于key的倹{Hashtable中keyvalue键值对均ؓobjectcdQ所以Hashtable可以支持Mcd的keyvalue键值对.
?哈希表的单操?/font>
在哈希表中添加一个keyvalue键值对QHashtableObject.Add(key,value);
在哈希表中去除某个keyvalue键值对QHashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键keyQ ?HashtableObject.Contains(key);
下面控制台程序将包含以上所有操作:
using System;
using System.Collections; file使用HashtableӞ必须引入q个命名I间
class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); file创徏一个Hashtable实例
ht.Add(E,e);dkeyvalue键值对
ht.Add(A,a);
ht.Add(C,c);
ht.Add(B,b);
string s=(string)ht[A];
if(ht.Contains(E)) file判断哈希表是否包含特定键,其返回gؓtrue或false
Console.WriteLine(the E keyexist);
ht.Remove(C);U除一个keyvalue键值对
Console.WriteLine(ht[A]);此处输出a
ht.Clear();U除所有元?br /> Console.WriteLine(ht[A]); file此处不会有M输出
}
}
?遍历哈希?/font>
遍历哈希表需要用到DictionaryEntry ObjectQ代码如下:
for(DictionaryEntry de in ht) filehtZ个Hashtable实例
{
Console.WriteLine(de.Key);de.Key对应于keyvalue键值对key
Console.WriteLine(de.Value);de.Key对应于keyvalue键值对value
}
?对哈希表q行排序
对哈希表q行排序在这里的定义是对keyvalue键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因ؓ我们无法直接在Hashtableq行对keyq行重新排列Q如果需要Hashtable提供某种规则的输出,可以采用一U变通的做法Q?br /> ArrayList akeys=new ArrayList(ht.Keys); file别忘了导入System.Collections
akeys.Sort(); file按字母顺序进行排?br /> for(string skey in akeys)
{
Console.Write(skey + );
Console.WriteLine(ht[skey]);排序后输?br /> }
补充Q?
HashTable 的g可以是一个集合,HashTable中可以存放Q何对? 所对应的Key,Value都是对应的对象类? 比如说HashTable里还能放HashTable.