Community discussion forum

C# Problem, IOException: The Process cannot access the file because it is in use by another process.

Tags: csharp, db, xml India
  • 1 year ago

    I am trying to populate a dataset with the results of an SQL Query...then...display those results in a browser, using XML and HTML.

    public static void naming()

    {

    string query = "SELECT * FROM tbl_File_Health_Check " + //@Sql statement stored in variable query

    "WHERE (File_Name LIKE '%~%' " +

    "or file_name Like '%#%'" +

    "or file_name Like '%&%'" +

    "or file_name Like '%*%'" +

    "or file_name Like '%{%'" +

    "or file_name Like '%}%'" +

    "or file_name Like '%\\%'" +

    "or file_name Like '%:%'" +

    "or file_name Like '%<%'" +

    "or file_name Like '%>%'" +

    "or file_name Like '%?%'" +

    "or file_name Like '%/%'" +

    "or file_name Like '%+%'" +

    "or file_name Like '%|%'" +

    "or file_name Like '%[%]%')"+ "ORDER BY File_Name";

    using (SqlConnection conn = new SqlConnection("Server=ServerName;Database=File_Health_Check;Trusted_Connection=True;")) //constructs SqlConnection object and stores Database Connection string

    {

    SqlCommand cmd = new SqlCommand(query, conn);

    conn.Open();

    try

    {

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);DataSet ds = new DataSet();

    adapter.Fill(ds);

    ds.DataSetName = "Queries";

    ds.Tables[0].TableName = "Naming Conventions";

    DirectoryInfo tempDir = new DirectoryInfo("C:\\");

    tempDir.CreateSubdirectory("tempWeb");

     

    XmlDataDocument Xmldoc = new XmlDataDocument(ds);

    XslCompiledTransform Xmltransform = new XslCompiledTransform();

    FileInfo tempFile = new FileInfo(@"C:\tempWeb\Queries.xsl");if (tempFile.Exists)

    {

    tempFile.Delete();

    }

    tempFile.Create();

     

    FileStream FileStore = tempFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

    FileStore.Close();

    StreamWriter writer = new StreamWriter(@"C:\tempWeb\Queries.html");

    writer.Close();

    Xmltransform.Load(
    @"C:\tempWeb\Queries.xsl"); Xmltransform.Transform(Xmldoc, null, writer);

    conn.Close();

    }

    catch (DirectoryNotFoundException e)

    {

    Console.WriteLine(e.Message);

    }

    catch (IOException e)

    {

    Console.WriteLine(e.Message);

    }

    catch (XsltException e)

    {

    Console.WriteLine(e.Message);

    }

     

    }

     

    Console.WriteLine("Database Read...");

    }

    The break occurs when trying to load the.Xsl file with this statement.

    Xmltransform.Load(@"C:\tempWeb\Queries.xsl");

     

    I've tried closing "tempFile" after creation...however there is no method definition for Close(); with FileInfo.

    To achieve some form of File closure...I stored "tempFile" within Filestream.  this however did not work.

    any help would be great.

  • 1 year ago

     Are you sure you want to delete and the recreate "C:\tempWeb\Queries.xsl"  ??? .... shouldn't this be the html file and not the xsl file?

    Joe 

  • 1 year ago

    This is a common problem with Windows operating  System.

    While performing the Delete/Rename/Move operations with files or directories, try to include the code in the loop like:

    while(status==true)

    {

    try

    {

    i++; 

    //Delete Stmt

    status = false;

    }

    catch(Exception ex)

    {

    if(i==3000)

    {

    status = false;

    }

    }

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback