点点滴滴
访问量:
sopper
博客园
首页
新随笔
新文章
联系
订阅
管理
posts - 84,comments - 117,trackbacks - 7
<
2008年6月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(5)
给我留言
查看留言
我参与的团队
北京.NET俱乐部(0/1553)
开源GIS学习(0/264)
我的标签
socket
(1)
线程
(1)
随笔档案
2008年6月 (1)
2008年5月 (2)
2008年4月 (1)
2008年3月 (3)
2008年2月 (1)
2008年1月 (2)
2007年12月 (9)
2007年11月 (1)
2007年6月 (7)
2007年5月 (3)
2007年3月 (8)
2007年1月 (15)
2006年12月 (2)
2006年11月 (8)
2006年10月 (1)
2006年4月 (19)
收藏夹
blog(2)
C#blog友情连接
C# Open Source
a
www.oazabir.com
开心就好【博客堂】
清清月儿
天道酬勤
最新随笔
1. 瑞星版本20.49.30的Bug
2. C++控件的数据交换和数据校验
3. VC6类向导对窗体按钮添加的消息映射代码
4. c/c++下常见的内存错误及其对策
5. CORBA_TAO的环境配置
6. 再议ManualResetEvent
7. 异步socket的线程分配(C#)
8. 你知道main函数参数的用处吗?
9. 你能确定这段代码的输出结果吗?
10. 小技巧避免项目中的大隐患
积分与排名
积分 - 39619
排名 - 1098
最新评论
1. re: C#转c++
楼主好羡慕你啊! 我喜欢c++和c那样底层的东西. 可是没有工作经验..找不到工作 只能被迫学习c#,说起来用ms定义好的东西是很方便........ 但是太方便了就是觉得恶心.特别恶心,明明需要自己...
--realpls
2. re: c# MD5加密算法的实例
if (s[i] < 16) { pwd = pwd + "0&qu...
--wintersday
阅读排行榜
1. C# 里List的用法(4255)
2. c# MD5加密算法的实例(2526)
3. ManualResetEvent用法 (2223)
4. C#连接mysql(1978)
5. 构造函数(1868)
评论排行榜
1. C#连接mysql(14)
2. 最基本的Socket编程 C#版(14)
3. 小技巧避免项目中的大隐患(13)
4. c# MD5加密算法的实例(11)
5. ManualResetEvent用法 (9)
C# 操作文件夹及文件
class
Program
{
/**/
///
<summary>
///
追加文件,文件不存在则创建,存在则向文件写数据
///
</summary>
///
<param name="users"></param>
///
<param name="content"></param>
public
static
void
WriteFile(
string
users,
string
content)
{
try
{
string
path
=
@"
测试.log
"
;
string
mDate
=
DateTime.Now.ToString();
if
(
!
File.Exists(path))
//
判断是否存在此文件
{
StreamWriter sw
=
File.CreateText(path);
sw.WriteLine(users
+
"
\t
"
+
mDate
+
"
\t
"
+
content);
sw.Close();
}
else
{
StreamWriter sw
=
File.AppendText(path);
sw.WriteLine(users
+
"
\t
"
+
mDate
+
"
\t
"
+
content);
sw.Close();
}
}
catch
(Exception ee)
{
throw
new
Exception(ee.Message);
}
}
/**/
///
<summary>
///
创建文件
///
</summary>
public
static
void
CreateFile()
{
string
path
=
"
测试1.log
"
;
try
{
if
(
!
File.Exists(path))
//
判断此文件是否存在,不存在则创建此文件
{
FileStream fs
=
File.Create(path);
//
string str = "测试";
//
Byte[] info = Encoding.UTF8.GetBytes(str);
Byte[] info
=
{
0
,
0
,
0
,
0
,
0
,
0
}
;
fs.Write(info,
0
, info.Length);
fs.Close();
}
}
catch
(Exception ee)
{
}
}
/**/
///
<summary>
///
copy文件
///
支持copy后重新命名
///
</summary>
public
static
void
CopyFile()
{
string
sourcePath
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\测试.log
"
;
//
源文件
string
objectPath
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log
"
;
//
目标文件
try
{
if
(
!
File.Exists(sourcePath))
//
源文件
{
return
;
}
if
(File.Exists(objectPath))
//
目标文件
{
return
;
}
File.Copy(sourcePath,objectPath);
//
这里的参数是两个文件路径,目标文件名可以不同于源文件名,效果是把源文件copy到新的路径,并且重新命名
}
catch
(Exception ee)
{ }
}
/**/
///
<summary>
///
移动文件
///
支持移动后重新命名
///
</summary>
public
static
void
MoveFile()
{
string
sourcePath
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\测试1.log
"
;
string
objectPath
=
@"
C:\Documents and Settings\5.txt
"
;
//
@"C:\Documents and Settings\测试.log";
try
{
if
(
!
File.Exists(sourcePath))
//
源文件
{
return
;
}
if
(File.Exists(objectPath))
//
目标文件
{
return
;
}
File.Move(sourcePath,objectPath);
//
这里的参数是两个文件路径,目标文件名可以不同于源文件名,效果是把源文件移动到新的路径,并且重新命名
}
catch
(Exception ee)
{}
}
/**/
///
<summary>
///
删除文件
///
</summary>
public
static
void
DeleteFile()
{
string
path
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log
"
;
try
{
if
(
!
File.Exists(path))
{
return
;
}
File.Delete(path);
}
catch
(Exception ee)
{}
}
/**/
///
<summary>
///
获取文件信息
///
</summary>
static
string
fileName
=
""
;
static
string
fileLength
=
""
;
static
string
fileTime
=
""
;
static
string
fileAttributes
=
""
;
public
static
void
FileInfo()
{
string
path
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log
"
;
FileInfo fi
=
new
FileInfo(path);
try
{
if
(
!
File.Exists(path))
{
return
;
}
fileName
=
fi.DirectoryName;
//
文件完整路径
fileLength
=
fi.Length.ToString();
//
文件长度
fileTime
=
fi.CreationTime.ToString();
//
文件创建的时间
fileAttributes
=
fi.Attributes.ToString();
//
属性,指得是右击文件-->属性-->常规下的高级-->存档和编制索引属性下的 可以存档文件选项
//
还有很多属性,用法差不多
}
catch
(Exception ee)
{}
}
//
以下是操作文件夹**************************************
/**/
///
<summary>
///
追加文件夹,不存在则创建
///
</summary>
///
<param name="path"></param>
///
<returns></returns>
public
static
string
CreateDirectory1(
string
path)
{
try
{
if
(Directory.Exists(path))
{
return
"
已经有这个路径
"
;
}
DirectoryInfo DirInfo
=
Directory.CreateDirectory(path);
//
用于创建指定目录的文件夹
return
"
路径创建成功!
"
;
}
catch
(Exception ee)
{
Console.WriteLine(ee.ToString());
return
"
N
"
;
}
}
/**/
///
<summary>
///
移动文件夹
///
支持重新命名
///
</summary>
public
static
void
MoveDirectory()
{
string
sourcePath
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\f
"
;
string
objectPath
=
@"
C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file
"
;
try
{
if
(
!
Directory.Exists(sourcePath))
{
return
;
}
if
(Directory.Exists(objectPath))
{
return
;
}
Directory.Move(sourcePath,objectPath);
}
catch
(Exception ee)
{}
}
/**/
///
<summary>
///
删除文件夹
///
</summary&g