C#如何在指定位置创建一个指定后缀指定内容的文件

来源:百度知道 编辑:UC知道 时间:2024/06/20 13:52:11
比如在一个button的click后,在F盘生成一个hahaha.txt的文件,里面内容是哈哈哈。

//文件所在的路径
string path = @"F:\hahaha.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("hahaha");
}
}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace createFile
{
class Program
{
static void Main(string[] args)
{
string fileName = @"f:/hahaha.txt";
FileStream fs = File.Create(fileName);
Byte[] info = new UTF8Encoding(true).GetBytes("哈哈哈");
fs.Write(info, 0, info.Length);
fs.Flush();
fs.Close();

}
}
}