如下代码,我在xxx文件夹下面建立XXX文件,这个事件写在load里面,为什么我每次load后,我文件里在的内容都会被覆盖。
如果不覆盖,该怎么改啊,谢谢了。
private void yqx_Load(object sender, EventArgs e)
{
string m_path=Application.StartupPath + "\\xxx\\xxx.csv" ;
if (File.Exists(m_path))
{
return;
}
else
{
m_path = Convert.ToString(File.Create((Application.StartupPath + "\\xxx\\xxx.csv")));
}
string mypath=Application.StartupPath + "\\xxx" ;
if (Directory.Exists(mypath))
{
return;
}
else
{
mypath = Convert.ToString(Directory.CreateDirectory(Application.StartupPath + "\\xxx"));
}
}
如果不覆盖,该怎么改啊,谢谢了。
private void yqx_Load(object sender, EventArgs e)
{
string m_path=Application.StartupPath + "\\xxx\\xxx.csv" ;
if (File.Exists(m_path))
{
return;
}
else
{
m_path = Convert.ToString(File.Create((Application.StartupPath + "\\xxx\\xxx.csv")));
}
string mypath=Application.StartupPath + "\\xxx" ;
if (Directory.Exists(mypath))
{
return;
}
else
{
mypath = Convert.ToString(Directory.CreateDirectory(Application.StartupPath + "\\xxx"));
}
}
string mypath = Application.StartupPath + "\\xxx"; if (!Directory.Exists(mypath))
{
Directory.CreateDirectory(mypath);
} if (!File.Exists(m_path))
{
File.Create(m_path);
}
应该是File.Create的问题
建议 File.Create(m_path).close();
string mypath = Application.StartupPath + "\\xxx"; if (!Directory.Exists(mypath))
{
Directory.CreateDirectory(mypath);
} if (!File.Exists(m_path))
{
File.Create(m_path).Close();
File.AppendAllText(m_path, "abc");
}
else
{
File.AppendAllText(m_path, ",def");
}