各位高手,请教几个文件操作的问题
1.首先在一个aspx页面中:判断在D盘中有没有一个叫report的文件夹,如果没有则建立一个report文件夹,并且将这个aspx页面所在的目录中的一个叫r.xml文件拷贝到report这个文件夹中去
2.如果在D盘中存在report文件夹,那么列出这个文件夹中所有的文件。
请大家帮我写写这些语句,谢谢了!!!
1.首先在一个aspx页面中:判断在D盘中有没有一个叫report的文件夹,如果没有则建立一个report文件夹,并且将这个aspx页面所在的目录中的一个叫r.xml文件拷贝到report这个文件夹中去
2.如果在D盘中存在report文件夹,那么列出这个文件夹中所有的文件。
请大家帮我写写这些语句,谢谢了!!!
using System.IO;class Test
{
public static void Main()
{
string path = @"d:\report\r.xml";
string path2 = path + "report";
try
{
using (StreamWriter sw = File.Create(path)) {} // Only do the Copy operation if the first file exists
// and the second file does not.
if (File.Exists(path))
{
if (File.Exists(path2))
{
Console.WriteLine("The target already exists");
}
else
{
// Try to copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
}
}
else
{
Console.WriteLine("The source file does not exist.");
}
}
catch
{
Console.WriteLine("Double copying is not allowed, as expected.");
}
}
}
先判断在D盘下是否有一个叫report的文件夹,如果没有,要建立它。建立好report文件夹后,在把该aspx文件所在目录中的一个r.xml文件拷贝到reprot这个文件夹中去!
最后,列出所有report文件夹中的文件(以后会写入很多个xml文件到这个文件夹中去)
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.CreateText(path + @"\r.xml");
.......................
}
catch (Exception e)
{
........ }
finally {}
应该是:
CurrentPath= @"d:\report\"; //设置当前目录
if(Directory.Exists(@"c:\UserDir\")==false) //若该目录不存在,创建该目录
Directory.CreateDirectory(@"c:\UserDir\");
LoadDir(CurrentPath); //初始化装入目录
private void LoadDir(string FullPath)
{
CurrentPath=FullPath;
ArrayList values = new ArrayList();
string [] MyFiles,MyDirs;
MyFiles = Directory.GetFiles(FullPath); //得到该目录下所有文件
values.AddRange(MyFiles); //加入文件
MyDirs= Directory.GetDirectories(FullPath); //得到该目录下所有目录
values.AddRange(MyDirs); //加入目录
FileList.DataSource=values; //设置数据源
FileList.DataBind(); //绑定数据
}