List Info

Thread: .net executeable not releasing a file




.net executeable not releasing a file
country flaguser name
United States
2007-05-02 09:06:19
Hi,
I have a .net executeable that rewrites a file. It does this
lightning fast and very well. However, it does not release
the lock on the file when it completes the task. So,
whenever it runs, I have to go in via processexplorer and
kill it. Its an extremely simple executeable (see below).
Anybody have any idea why Its not releasing the lock?

Thanks in advance!

using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;



namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {  
            Console.Out.WriteLine("starting");

            Thread.GetDomain().UnhandledException += new
               
UnhandledExceptionEventHandler(Application_UnhandledExceptio
n);
          
            rewriteFile(args[0],
                        args[1]);
            Console.Out.WriteLine("finished");

            Thread.Sleep(2000);
        }

        public static void
Application_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
        {
           
System.Diagnostics.EventLog.WriteEntry("file Rewriter
Errors", e.ToString());
            myReadStream.Close();
            myWriteStream.Close();
        }
       
        static void rewriteFile(string fromFile, string
toFile)
        {
            StreamReader myReadStream = new
StreamReader(fromFile);
            StreamWriter myWriteStream = new
StreamWriter(toFile);
            
            string myLine = myReadStream.ReadLine();
           
            Regex r1 = new Regex("<Fulltext(
wordCount="[0-9]*")?>");
            Regex r2 = new
Regex("<Subdivision>[^<]*</Subdivision>&
quot;);
            
            while (myLine != null)
            {
                myLine =
myLine.Replace("<IntroText>",
"<IntroText><![CDATA[");
                myLine =
myLine.Replace("</IntroText>",
"]]></IntroText>");

                myLine = r1.Replace(myLine,
"<Fulltext><![CDATA[");
                myLine =
myLine.Replace("</Fulltext>",
"]]></Fulltext>");
               
                myLine =
myLine.Replace("<FurtherReadings>",
"<FurtherReadings><![CDATA[");
                myLine =
myLine.Replace("</FurtherReadings>",
"]]></FurtherReadings>");

                myLine = r2.Replace(myLine, "");

                myLine =
myLine.Replace("premium="true"",
"Premium="true"");
                myLine =
myLine.Replace("premium="false"",
"Premium="false"");

                myWriteStream.WriteLine(myLine);    

                myLine = myReadStream.ReadLine();
            }

            myReadStream.Close();
            myWriteStream.Close();

        }
    }
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~|
Deploy Web Applications Quickly across the enterprise with
ColdFusion MX7 & Flex 2
Free Trial 
http://www.adobe.com/products/coldfusion/flex2/?sdid=RV
JU

Archive: http://www.houseoffusion.com/groups/Net-T
alk/message.cfm/messageid:4038
Subscription: http://www.houseoffusion.com/groups/Net-Talk/subscribe
.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/u
nsubscribe.cfm?user=22184.15789.44

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )