string FilePath;
            string info;
            FilePath = "d:\\aaabbb.txt";
            info = "哪个进程占用文件";
            
            //创建文件
    if (!File.Exists(FilePath))
                File.Create(FilePath);     //写文件
            StreamWriter sr = null;
            sr = new StreamWriter(FilePath);
            sr.Write(info);
            sr.Close();
            
    //删除文件
            if (File.Exists(FilePath))
                File.Delete(FilePath);
----------------------------------------------------------
第一次运行正常,重复执行的时候,提示"文件“d:\aaabbb.txt”正由另一进程使用,因此该进程无法访问该文件。"

解决方案 »

  1.   

    把streamWriter放到一个using()中使用或者使用finally来保证streamWriter被正确释放了。然后再看看!
      

  2.   

    File.Create 返回了一个FileStream .这个FileStream 没有关闭.
      

  3.   

                string FilePath; 
                string info; 
                FilePath = "d:\\aaabbb.txt"; 
                info = "哪个进程占用文件"; 
                
                //创建文件 
                  if (!File.Exists(FilePath)) 
                    FileStream fs=File.Create(FilePath);             //写文件 
                  StreamWriter sr = null; 
                sr = new StreamWriter(fs); 
                sr.Write(info); 
                sr.Close(); 
                fs.Close();//这里没有关闭filestream;
                
                //删除文件 
                 if (File.Exists(FilePath)) 
                    File.Delete(FilePath); 
      

  4.   

    把你的代码改了.
    //创建文件 
                if (!File.Exists(FilePath))
                {
                    FileStream fs = File.Create(FilePath);
                    fs.Close();
                }
      

  5.   

    你的写文件代码都多余// Create the file.
                using (FileStream fs = File.Create(path))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                    // Add some information to the file.
                    fs.Write(info, 0, info.Length);
                }
      

  6.   

    该文件被占用,很明显被占用了,我给你个函数 private void InsertDataToTxt(string str)
            {
                string folder, file;
                folder = Server.MapPath(@"/RemoteInfo");
                file = folder+@"\"+ DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + ".txt";
                try
                {
                    DirectoryInfo DI = new DirectoryInfo(folder);
                    if (DI.Exists.ToString() == "False")
                    {
                        Directory.CreateDirectory(folder);
                    }
                    if (File.Exists(file))
                    {
                        StreamWriter SW = new StreamWriter(file, true, System.Text.Encoding.GetEncoding("GB2312"));
                        SW.WriteLine(str);
                        SW.Flush();
                        SW.Close();
                    }
                    else
                    {
                        StreamWriter SW = new StreamWriter(file, false, System.Text.Encoding.GetEncoding("GB2312"));
                        SW.WriteLine(str);
                        SW.Flush();
                        SW.Close();
                    }
                }
                catch
                { }
            }自己再加以改下