初学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()
{