C#文件和文件夾操作
http://www.csref.cn/vs100/method/System-IO-BinaryReader-ReadBytes.html
string basePath = AppDomain.CurrentDomain.BaseDirectory + "\\NewPhoneBillRecord\\";
//判斷該路徑下文件夾是否存在,不存在的情況下新建文件夾
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
}
string postPath = basePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";//路徑+文件名
using (StreamWriter file = new StreamWriter(@postPath, false))
{
file.WriteLine(lines);
}
StreamWriter 實現一個 TextWriter,使其以一種特定的編碼向流知中寫入字符
例StreamWriter SWriter = new StreamWriter(path,append);
表示-----
path 要寫入的完整文件路徑。
append 確定是道否將數據追加到文件。如果該文件存在,並回且 append 為 false,
則該文件被改寫。如果該文件存在,並且 append 為 true,則數據被追加到該文件中。
否則,將創建新文答件
C#清空txt內容,並追加內容
1、將txt內容清空
public void ClearTxt(String txtPath)
{
String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt\" + txtPath;
FileStream stream = File.Open(appDir, FileMode.OpenOrCreate, FileAccess.Write);
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);
stream.Close();
}
2、向txt中追加內容
public void SaveFile(string str,String txtPath,bool saOrAp) {
String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt\" + txtPath;
StreamWriter sw = new StreamWriter(appDir, saOrAp);//saOrAp表示覆蓋或者是追加
sw.WriteLine(str);
sw.Close();
}
3、c#創建目錄和文件夾,數據寫入並生成txt文件
c#創建目錄:
// 獲取程序的基目錄。
System.AppDomain.CurrentDomain.BaseDirectory
// 獲取模塊的完整路徑。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
// 獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄。
System.Environment.CurrentDirectory
// 獲取應用程序的當前工作目錄。
System.IO.Directory.GetCurrentDirectory()
// 獲取和設置包括該應用程序的目錄的名稱。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
// 獲取啟動了應用程序的可執行文件的路徑。
System.Windows.Forms.Application.StartupPath
// 獲取啟動了應用程序的可執行文件的路徑及文件名
System.Windows.Forms.Application.ExecutablePath
//組成新的路徑
string path=System.Windows.Forms.Application.StartupPath+"\\DownFile\\";
//判斷該路徑下文件夾是否存在,不存在的情況下新建文件夾
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//生成txt文件,將json字符串數據保存到txt文件
string postPath=path+DateTime.Now.ToString("yyyyMMddHHmmss")+".txt";//路徑+文件名
byte[] bytes=null;
bytes=Encoding.UTF8.GetBytes(Obj.ToString())//Obj為json數據
FileStream fs=new FileStream(postPath,FileMode.Create);
fs.Write(bytes,0,bytes.Length);
fs.Close();
將字符串寫入文本
public void writeTextToFile(string lines)
{
#region 將字符串數組寫入到文件,每個元素為一行
//string[] lines = { "First line", "Second line", "Third line" };
//string textPath = Configuration.AppSetting();
//File.WriteAllLines(@"C:\Users\Public\TsetFolder\WriteLines.txt", lines);
#endregion
#region 將字符串寫入到文件
//string text = "A class is the most powerful data type in C#. Like a structure, " + "a class defines the data and behavior of the data type. ";
//File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
#endregion
#region 使用using語句動態寫入到文件
//using (StreamWriter file = new StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
//{
// foreach (string line in lines)
// {
// if (!line.Contains("Second"))
// {
// file.WriteLine(line);
// }
// }
//}
#endregion
//獲取程序的基目錄並組成新路徑
string basePath = AppDomain.CurrentDomain.BaseDirectory + "NewPhoneBillRecord\\";
//判斷該路徑下文件夾是否存在,不存在的情況下新建文件夾
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
}
string postPath = basePath + DateTime.Now.ToString("yyyyMMdd") + ".txt";//路徑+文件名
using (StreamWriter file = new StreamWriter(@postPath, false)) //false:改寫,true:追加
{
file.WriteLine(lines);
}
}
