c#进程无法访问该文件,因为它正由另一个进程使用

时间:2013-07-01 18:45:19

标签: file-io filestream streamwriter

我写了这段代码。

        string filepath = (string)command.ExecuteScalar();
        string gfile = Server.MapPath("//" + filepath);

        connection.Close();


        string path = newFile(aid);


        string AttributeDeclaration = "@ATTRIBUTE";
        string AttributeDeclaration2 = "@attribute";
        string Relation = "@RELATION";
        string Relation2 = "@relation";
        string Data = "@DATA";
        string Data2 = "@data";


        string line;


        using (FileStream fsReader = new FileStream(gfile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            using (StreamReader sr = new StreamReader(fsReader))
            {
                while ((line = sr.ReadLine()) != null)
                {


                    if (line.StartsWith(Relation) | line.StartsWith(Relation2))
                    {

                        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            fs.Close();
                        }
                        try
                        {
                            using (StreamWriter writer = new StreamWriter(path, true))
                            {
                                writer.WriteLine(line);
                                writer.Flush();
                                writer.Close();
                                writer.Dispose();
                            }
                        }
                        catch (IOException)     
                        {

                        }



                    else if (line.StartsWith(AttributeDeclaration) | line.StartsWith(AttributeDeclaration2))
                    {

                        var data = line.Split(new Char[] { ' ', '\t' }, 3);
                        string attri = (data[0]);
                        string name = (data[1]);
                        string type = (data[2]);
                        Save(aid, attri, name, type);
                    }


                }

                sr.Close();
            }

        }
}
    }




 private string newFile(int aid)
    {
        string folderName = @"C:\Users\valentina\Downloads\Desktop\web respository"; //@"C:\Users\valentina\Documents\Visual Studio 2010\Projects\WebRepository3\WebRepository3\files";
        string pathString = System.IO.Path.Combine(folderName, "CreateFiles");
        string fileName = System.IO.Path.GetRandomFileName();

        pathString = System.IO.Path.Combine(pathString, fileName);
        string newfilePath = System.IO.Path.ChangeExtension(pathString, ".txt");
        System.IO.File.Create(newfilePath);


        SqlConnection con = new SqlConnection("Data Source=VALENTINA-PC\\SQLEXPRESS;Initial Catalog=repository_db;Integrated Security=True");
        SqlCommand command = con.CreateCommand();


        command.CommandText =
        @"INSERT INTO new_file
        (set_id, path) 
      VALUES 
        (@set_id, @path)";

        con.Open();


        command.Parameters.Add("@set_id", SqlDbType.Int).Value = aid;
        command.Parameters.Add("@path", SqlDbType.NVarChar, 1000).Value = newfilePath;

        command.ExecuteNonQuery();

        con.Close();
        return (newfilePath);
    }

但我有错误进程无法访问该文件,因为它正被另一个进程使用。错误在于使用(FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))。如果我删除它,错误行将在StreamWriter下面。我用ReadAllLInes尝试了一切,没有关闭...... Cam任何人都帮我解决了这个问题。

代码必须是从txt,文件读取,然后是从其他txt文件中的关系写入开始的行。

1 个答案:

答案 0 :(得分:0)

尝试使用DirectoryInfo创建目录,然后使用File:

添加文本
        string filePath ="Your_file_path.txt"; 
        DirectoryInfo FileCreator = new DirectoryInfo(filePath);

        File.AppendAllText(filePath, "some conetnt");
        File.AppendAllText(filePath, "make sure you spell content wrong");