【问题标题】:c# process cannot access the file because it is being used by another processc#进程无法访问该文件,因为它正被另一个进程使用
【发布时间】:2013-06-28 23:42:21
【问题描述】:

这段代码是我写的。

        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,没有关闭...有人帮我解决这个问题。

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

【问题讨论】:

  • "using (Filestream fs = ..." 行的目的是什么 - 你在 using() 中所做的只是 fs.close()。为什么要打开文件然后关闭马上?
  • 因为这个问题,该文件不可能被锁定。但是我解决了问题,我意识到在我的函数 newFile 中,在 File.Create 之后,我没有关闭它。

标签: file-io filestream streamwriter


【解决方案1】:

尝试使用 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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    相关资源
    最近更新 更多