git : https://raw.githubusercontent.com/todomato/CSharp_IO/master/Program.cs
static void Main(string[] args) { //取得Bin資料夾 var bin = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///", ""); var path = Path.Combine(Path.GetDirectoryName(bin),"Demo"); //判斷資料夾位置 if (!Directory.Exists(path)) { //建立資料夾 Directory.CreateDirectory(path); } #region 文件相關 Console.WriteLine(Path.GetDirectoryName(path)); //取得資料夾稱, D:\TestDir Console.WriteLine(Path.Combine(new string[] { @"D:\", "BaseDir", "SubDir", "TestFile.txt" })); //產生路徑 #endregion //判斷檔案位置 var filename = "test.txt"; //結合路徑 var filePath = Path.Combine(path, filename); //判斷資料夾位置 if (!File.Exists(filePath)) { //建立檔案 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); } }