如下代码,我在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"));
            }
}

解决方案 »

  1.   

                string m_path = Application.StartupPath + "\\xxx\\xxx.csv";
                string mypath = Application.StartupPath + "\\xxx";            if (!Directory.Exists(mypath))
                {
                    Directory.CreateDirectory(mypath);
                }            if (!File.Exists(m_path))
                {
                    File.Create(m_path);
                }
      

  2.   

    我也没看到你的代码里有写文件的操作啊。
    应该是File.Create的问题
      

  3.   

    这样可以创建一个文件,但是这个文件是无法被其他应用程序使用的。
    建议  File.Create(m_path).close();
      

  4.   

                string m_path = Application.StartupPath + "\\xxx\\xxx.csv";
                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");
                }
      

  5.   

    谢谢,herbt,刚刚找到问题了,自己低级错误,代码本身是没有问题的,犯了“鸡和蛋”的错误。我是先判断文件夹,再判断文件。是先有鸡,还是先有蛋。杯具,杯具。