//检查文件,如果文件不存在则创建 private void ExistsFile(string FilePath) { //if(!File.Exists(FilePath)) //File.Create(FilePath); //以上写法会报错,详细解释请看下文......... if (!File.Exists(FilePath)) { FileStream fs = File.Create(FilePath); fs.Close(); } } private void Button2_Click(object sender, System.EventArgs e) { ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在