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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM