我寫了下面的程序來創建一個.txt文件到一個特定的目錄。但我無法創建該文件。我甚至改變了字符串fileName = @"D:\\abc.txt";
的路徑,程序編譯,但沒有創建到該特定目錄的文件。用c創建一個.txt文件到一個特定的目錄#
有誰能幫我弄清楚什麼可能是錯誤?
namespace SimpleLicense
{
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\\Temp\\abc.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (StreamWriter sw = File.CreateText(fileName))
{
sw.WriteLine("Installed Date: {0}", DateTime.Now.ToString());
sw.WriteLine("Thermo Licensing System file");
DateTime newDate = DateTime.Now.AddDays(31);
sw.WriteLine("License Expires After"+newDate);
// sw.WriteLine("Add ");
// sw.WriteLine("Done! ");
}
// Write file contents on console.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
}
}
你得到或發現異常?如果是這樣,它說什麼? – martennis
你知道,當你使用'@'時,你不需要加倍你的斜槓,對吧? – dasblinkenlight
也用新的StreamWriter(「fileName」)替換File.CreateText(fileName) –