0

我写了这段代码。

        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 文件。

4

1 回答 1

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");
于 2017-06-19T16:03:21.753 回答