public void WriteFile(string str1, string str2) 
        {
            try
            {
                StreamWriter sw = new StreamWriter(str2);
                sw.WriteLine(str1);
                sw.Flush();
                sw.Close();
            }
            catch
            {
                MessageBox.Show("我**!");
            }
            //StreamWriter sr;
            //sr = File.CreateText(str2);
            //sr.WriteLine(str1);
            //sr.Close();
        }很奇怪很无奈,软件随机启动,上面函数创建文本文件无效,文件存在他不覆盖,文件不存在他创建不出来,又不报错不走catch!!!软件关闭在打开就正常了!!!!!!???? 

解决方案 »

  1.   

    F9设置断点,F10单步调试  F11进函数
      

  2.   

    F9设置断点,F10单步调试  F11进函数 用不明白...
      

  3.   


    Application.StartupPath + "\\" + file_name_2
      

  4.   

    先用FileStream文件流试试FileStream filename = new FileStream(str2, FileMode.Create);
    StreamWriter wr = new StreamWriter(filename);
      

  5.   

    楼主看看
    C:\Documents and Settings\当前用户名
    这个路径下面,有没有你创建的文件。 
      

  6.   


    public static bool WriteFile(byte[] FileContent,string FilePath)
            {
                FileStream fs = null;
                bool boRetVal = true;
                try
                {
                    fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(FileContent, 0, FileContent.Length);
                }
                catch (Exception)
                {
                    boRetVal = false;
                }
                finally
                {
                    if (fs != null) { fs.Flush(); fs.Close(); fs.Dispose(); }
                }
                return boRetVal;
            }
      

  7.   

    楼主根本没有些操作文件的流,更别说创建了。建议理解一下FileStream
      

  8.   

    StreamWriter sw = new StreamWriter(new FileStream(...)); 
      

  9.   

    给你一个生成文件的类
     
    public static void CreateFile( String template, String path )
    {
    System.IO.StreamWriter objSW = null;
    try
    {
    objSW = new System.IO.StreamWriter( path, false, System.Text.Encoding.UTF8 );
    objSW.Write( template );
    objSW.Flush();
    objSW.Close();
    CNCDotNet.Really_Simple_Syndication.Asynchronous.Init_App();
    }
    catch( Exception ex )
    {
    ErrorCaptor.Descendant("创建文件异常", ex.ToString());
    }
    finally
    {
    if( objSW != null )
    {
    objSW.Close();
    objSW = null;
    }
    }
    }
      

  10.   

    因为我以前在用C++写随机启动程序的时候也遇到过类似问题,后来使用GetModuleFileName()来定位程序路径的,不过在C#中调用这个系统函数就比较麻烦了
      

  11.   

    对不起啊,读取用了Application.StartupPath 写入却没用这个,马虎了...各位,找到问题了,其他哥哥都没注意问题的关键,就是:软件关闭在打开就正常了,是路径问题,诶,真马虎..