using System;
using System.IO;
using System.Collections;
namespace FileOperation
{
public class TestFile
{
public static void MakeDirectory()
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/";
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
//
Console.WriteLine (strPath);
}
//创建一个文件夹在目录下
public static void DeleteDirectory()
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/";
if (Directory.Exists(strPath))
{
Directory.Delete(strPath, true);
}
}
//删除以存在的文件夹
public static void MoveAndCopyDirectory()
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/TT/";
string strPath2 = Directory.GetCurrentDirectory();
strPath2 += "/kk/GT/";
//
Console.WriteLine (strPath);
Directory.Move(strPath2, strPath);
}
//改变文件夹名称
public static void ChooseTime()
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/";
if (Directory.Exists(strPath))
{
DateTime pTime = Directory.GetCreationTime(strPath);
Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
pTime = DateTime.Parse("2016/06/16 00:00:00");
Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
}
}
//更改文件创建时间
public static bool MakeFileEx(string strPath, ref byte[] pBuffer, bool isBinary = true)
{
bool isRet = false;
FileStream pFile = null;
const int nMaxWriteSize = 65535;
do
{
if (File.Exists(strPath))
{
break;
}
if (pBuffer == null || pBuffer.Length <= 0)
{
break;
}
pFile = File.Create(strPath, pBuffer.Length, FileOptions.Asynchronous);
if (pFile == null)
{
break;
}
if (isBinary)
{
BinaryWriter pWrite = new BinaryWriter(pFile);
if (pWrite == null)
{
break;
}
int nWriteCount = 0;
long nFileLen = pFile.Length;
while (nWriteCount < pBuffer.Length)
{
pWrite.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
pWrite.Flush();
nWriteCount += (int)(pFile.Length - nFileLen);
}
}
else
{
int nWriteCount = 0;
long nFileLen = pFile.Length;
while (nWriteCount < pBuffer.Length)
{
pFile.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
pFile.Flush();
nWriteCount += (int)(pFile.Length - nFileLen);
}
}
isRet = true;
} while (false);
if (pFile != null)
{
pFile.Flush();
pFile.Close();
}
return isRet;
}
public static string GetPath(string value)//获取路径
{
string strPath = Directory.GetCurrentDirectory();
if (!value.Substring(0, 1).Equals("/"))
{
strPath += "/";
}
strPath += value;
return strPath;
}
public static void MakeFile()//文件写入
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/TT";
FileStream pFile = File.Create((strPath + "/MengBi.txt"), 100, FileOptions.Asynchronous);
string strBuffer = "这是一个蒙蔽的程序猿蒙蔽的写入";
byte[] pBuffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
pFile.Write(pBuffer, 0, pBuffer.Length);
pFile.Flush();
pFile.Close();
}
//创建一个文本并写入
public static bool TestRead()
{
string strPath = Directory.GetCurrentDirectory();
strPath += "/kk/TT/1.txt";
FileStream pFile = new FileStream(strPath, FileMode.Open);//读取文件
//FileStream Mytxt = File.OpenRead(strPath);//第二种读取
if (pFile == null)
{
Console.WriteLine("file open error!");
return false;
}
byte[] pBuffer = new byte[(int)strPath.Length];//建立一个缓存池
int ReadLen = pFile.Read(pBuffer, 0, pBuffer.Length);//把这个流读进缓冲池中去
pFile.Close();
if (ReadLen > 0)
{
string A = System.Text.Encoding.UTF8.GetString(pBuffer);
Console.WriteLine("1.txt:{0}", A);
return true;
}
return false;
}
public static bool TestReadEx(string strPath, bool isBinary = true)
{
bool isRet = false;
const int nMaxReadSize = 65535;
byte[] pBuffer = new byte[nMaxReadSize];
FileStream m_pReadFile = new FileStream(strPath, FileMode.Open);
do
{
if (!File.Exists(strPath))
{
break;
}
if (pBuffer == null || pBuffer.Length <= 0)
{
break;
}
if (m_pReadFile == null)
{
break;
}
if (isBinary)
{
BinaryReader pRead = new BinaryReader(m_pReadFile);
if (pRead == null)
{
break;
}
int nReadCount = 0;
while (nReadCount < m_pReadFile.Length)
{
pRead.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
nReadCount += (int)pBuffer.Length;
}
}
else
{
int nReadCount = 0;
while (nReadCount < m_pReadFile.Length)
{
m_pReadFile.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
nReadCount += (int)pBuffer.Length;
}
}
isRet = true;
} while (false);
return isRet;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)