我要遍历某目录下的所有文件及其子文件夹下的所有文件(),然后把其内容存放到一个txt文件中.
现在报错说,txt文件正在由另一个进程访问.哪位仁兄帮忙解决一下代码如下:
private void readfile(string dir,string doc,string extType)
{
string[] names=System.IO.Directory.GetFiles(dir,"*."+extType);
//遍历文件
if(names.Length !=0)
{
string csFile;
string title="//============";
string titleEnd="===========";
System.IO.FileStream fs;
System.IO.StreamReader reader;
System.IO.StreamWriter writer;
string content="";
for(int i=0;i<names.Length;i++)
{
//apsx file
try
{
fs=new System.IO.FileStream(names[i].ToString(), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
reader=new System.IO.StreamReader(fs);
content=reader.ReadToEnd();
reader.Close();
fs.Close(); writer=new System.IO.StreamWriter(doc,true);
writer.WriteLine(title + names[i].ToString() + titleEnd);
writer.WriteLine(content);
writer.Flush();
writer.Close();
//fs.Close(); //aspx.cs file
csFile=names[i].ToString()+".cs";
fs=new System.IO.FileStream(csFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
reader=new System.IO.StreamReader(fs);
content=reader.ReadToEnd();
reader.Close();
fs.Close();
writer=new System.IO.StreamWriter(doc,true);
writer.WriteLine(title + csFile + titleEnd);
writer.WriteLine(content);
writer.Flush();
writer.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//遍历目录
string[] subDirectorys=System.IO.Directory.GetDirectories(dir);
if(subDirectorys.Length !=0)
{
for(int i=0; i<subDirectorys.Length; i++)
{
readfile(subDirectorys[i].ToString(),doc,extType);
}
}
}
现在报错说,txt文件正在由另一个进程访问.哪位仁兄帮忙解决一下代码如下:
private void readfile(string dir,string doc,string extType)
{
string[] names=System.IO.Directory.GetFiles(dir,"*."+extType);
//遍历文件
if(names.Length !=0)
{
string csFile;
string title="//============";
string titleEnd="===========";
System.IO.FileStream fs;
System.IO.StreamReader reader;
System.IO.StreamWriter writer;
string content="";
for(int i=0;i<names.Length;i++)
{
//apsx file
try
{
fs=new System.IO.FileStream(names[i].ToString(), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
reader=new System.IO.StreamReader(fs);
content=reader.ReadToEnd();
reader.Close();
fs.Close(); writer=new System.IO.StreamWriter(doc,true);
writer.WriteLine(title + names[i].ToString() + titleEnd);
writer.WriteLine(content);
writer.Flush();
writer.Close();
//fs.Close(); //aspx.cs file
csFile=names[i].ToString()+".cs";
fs=new System.IO.FileStream(csFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
reader=new System.IO.StreamReader(fs);
content=reader.ReadToEnd();
reader.Close();
fs.Close();
writer=new System.IO.StreamWriter(doc,true);
writer.WriteLine(title + csFile + titleEnd);
writer.WriteLine(content);
writer.Flush();
writer.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//遍历目录
string[] subDirectorys=System.IO.Directory.GetDirectories(dir);
if(subDirectorys.Length !=0)
{
for(int i=0; i<subDirectorys.Length; i++)
{
readfile(subDirectorys[i].ToString(),doc,extType);
}
}
}
解决方案 »
- repeater生成的控件id问题
- 有谁研究过NeatUpload的源码(上传进度条)
- 使用ISNET出现的问题
- |zyciis| 在做URL改写的时候,我们如何判断一个页面是否是要改写回来的呢,谢谢
- 使用ADO.net数据数据服务有时间限制?
- 超郁闷!我把asp.net 程序上传到虚拟空间,然后打开页面,直接显示HTML源代码,是怎么回事?
- asp.net播放视频文件MP4的问题,请高手指点,谢谢
- 不重新发送信息,则无法刷新网页。请单击“重试”再次发送信息,或单击“取消”返回正查看的页。
- 能不能将XML代码写入数据库的某个字段,在将数据库字段当中的XML代码读出?请求指点!
- 正则表达式,请问控制输入为大写字母A-Z(除I、O以外)和数字的什么判断呢
- 问个sql语句问题
- 问个<INPUT>File控件的问题
http://fenglin.xland.cn
------------------------