Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I am trying to read through a streamreader but it says it cant access the file because it is used by some other process the file is on my c drive and I am not opening it below is my method

when I debug I am seeing the records populating

C#
public int RunConversion()
{
    string[] hl7Files = Directory.GetFiles(inputFilePath);
   // List<string> list = new List<string>();

    string line = string.Empty;

    foreach (string hl7 in hl7Files)
    {
        try
        {
            using (StreamReader sr = new StreamReader(hl7))
            {
               DataRow dtRow = demoTable.NewRow();
                                        //int count = 0;
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();

                    string[] lineData = line.Split('|');

                    for (int i = 0; i < lineData.Length - 1; i++)
                    {
                        demoTable.Rows.Add(dtRow);\\ when debug I dont see any values when I put a watch window for Rows.
                        dtRow = demoTable.NewRow();

                 }//while
            }//using

            if (File.Exists(hl7))
            {

                File.Copy(hl7, Path.Combine(archiveFilePath, Path.GetFileName(hl7) + ".Processed"), true);

                File.Delete(hl7);

            }

        }
        catch (Exception ex)
        {

            if (File.Exists(hl7))
            {

                File.Copy(hl7, hl7 + ".Failed", true);

                File.Delete(hl7);

            }

            StringBuilder message = new StringBuilder();

            message.Append("Error occurred in line = " + line + "  when processing file " + hl7);

            message.Append("\nError Details:\n");

            message.Append(ex.Message.ToString());

            message.Append("\nNo Demographics File Generated!!!\n");

            throw new Exception(message.ToString());

        }

    }

    return 1;
}


can anybody help me.

Thanks
Posted
Updated 17-Jan-14 9:48am
v2
Comments
Ron Beyer 17-Jan-14 15:49pm    
Did you copy/paste this code from your editor into here? What you have above would never compile, you are missing closing braces on the for loop...
Mary Abraham 17-Jan-14 22:39pm    
yes I copied and pasted

1 solution

Please see my past answer, it can help you: File used by another process exception[^].

—SA
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Jan-14 22:03pm    
5!
Sergey Alexandrovich Kryukov 17-Jan-14 22:09pm    
Thank you, KARTHIK.
—SA
Mary Abraham 21-Jan-14 22:50pm    
Thanks to all
Sergey Alexandrovich Kryukov 21-Jan-14 23:55pm    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900