vale
vale

Reputation: 1

c# process cannot access the file because it is being used by another process

I wrote this code.

        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);
    }

But I have error process cannot access the file because it is being used by another process. The error is in using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)).. If I delete it the error line is bellow StreamWriter. I tried everything, with ReadAllLInes, without Close... Cam anyone help me solve me this problem.

The code must be doing that it reads from txt, file and then the line what starts with relation write in other txt file.

Upvotes: 0

Views: 4245

Answers (1)

Benjamin Ronneling
Benjamin Ronneling

Reputation: 821

Try to create your directory using DirectoryInfo then you can add your text using 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");

Upvotes: 1

Related Questions