我试图把所有这些字符串放在一起放到我的程序保存文档的路径中。没什么奇特的。但每次我在调试时保存文件时,它都会创建一个以该文件命名的文件夹,而不会执行其他任何操作。我觉得这是一个简单的问题,但我无法找到如何解决它。请帮助!C#文件保存问题
我的代码
private void btnSave_Click(object sender, EventArgs e)
{
string strNotes = rtbNotes.Text.ToString();
string strUser = txtUser.Text.ToString() + "\\";
string strClass = txtClass.Text.ToString() + "\\";
string strDate = DateTime.Today.Date.ToString("dd-MM-yyyy");
string strLocation = "C:\\Users\\My\\Desktop\\Notes\\";
string strType = txtType.Text.ToString();
string strFile = strLocation + strUser + strClass + strDate;
string subPath = strFile + "." + strType;
bool isExists = System.IO.Directory.Exists(subPath);
if (!isExists)
System.IO.Directory.CreateDirectory(subPath);
System.IO.File.WriteAllText(strFile, strNotes);
}
您是否检查路径结果? subPath看起来像一个文件不是目录 – Sam
此代码应创建一个全名+类型的目录。然后,它应该创建一个没有包含strNotes扩展名的文件。 (除非发生错误) – 2013-10-26 01:34:23