【问题标题】:C# File locked after making itC#文件在制作后被锁定
【发布时间】:2013-03-21 01:06:37
【问题描述】:

我正在制作一个 Windows 服务,其中一部分是使用 FileStream 和 StreamWriter 制作一个 .eml/txt 文件,它运行良好,可以完成我需要它做的所有事情。唯一的问题是它生成的新文件由于某种原因之后仍然被服务使用,我不知道为什么。有什么线索吗?修复?

我认为它必须与 FileStream 和 StreamWriter 有关,因为它们是唯一接触新文件的东西。提前致谢!

Service1.cs

 public partial class emlService : ServiceBase
{
    Boolean _isRunning;

    public emlService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("emlServiceSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "emlServiceSource", "emlServiceLog");
        }
        eventLog1.Source = "emlSerivceSource";
        eventLog1.Log = "emlServiceLog";
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
        Thread NewThread = new Thread(new ThreadStart(runProc));
        _isRunning = true;
        //Creates bool to start thread loop
        if (_isRunning)
        {
            NewThread.Start();
        }
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("In OnStop");
        _isRunning = false;             
    }

    protected override void OnContinue()
    {

    }

    public void runProc()
    {         

        //Directory of the drop location
        DirectoryInfo drop = new DirectoryInfo(@"C:\inetpub\mailroot\drop");

        //Directory of the pickup location
        string destpath = @"C:\inetpub\mailroot\pickup\";

        //Inits ParserCommands and DropDirectory object
        ParserCommands parserObject = new ParserCommands();

        //Inits array to hold number of messages in drop location
        FileInfo[] listfiles;

        //Inits CFG
        string conf_mailsender = ConfigurationSettings.AppSettings["conf_mailsender"];
        string conf_rcpt = ConfigurationSettings.AppSettings["conf_rcpt"];
        string conf_username_and_password = ConfigurationSettings.AppSettings["conf_username_and_password"];
        string conf_sender = ConfigurationSettings.AppSettings["conf_sender"];
        string conf_raport = ConfigurationSettings.AppSettings["conf_raport"];

        //Loop that never ends
        while (true) 
        {
            Thread.Sleep(1000);
            //Checks if there is a message waiting to be processed and begins processing if so
            listfiles = drop.GetFiles();
            if (listfiles.Length >= 1)
            {
                for (int j = 0; j <= (listfiles.Length - 1); j++)
                {
                    //Gives it time to breathe
                    Thread.Sleep(250);

                    //Gets each line of the original .eml into a string array
                    try
                    {

                        //reader.

                        var lines = File.ReadAllLines(listfiles[j].FullName);
                        string[] linestring = lines.Select(c => c.ToString()).ToArray();

                        //Opens up steam and writer in the new dest, creates new .eml file
                        FileStream fs = new FileStream(destpath + listfiles[j].Name, FileMode.CreateNew);
                        StreamWriter writer = new StreamWriter(fs);

                        //Writes all .eml content into buffer
                        writer.WriteLine("x-sender: " + conf_mailsender);
                        writer.WriteLine("x-receiver: " + conf_rcpt);
                        writer.WriteLine(linestring[2]);
                        writer.WriteLine(linestring[3]);
                        writer.WriteLine(linestring[4]);
                        writer.WriteLine(linestring[5]);
                        writer.WriteLine(linestring[6]);
                        writer.WriteLine(linestring[7]);
                        writer.WriteLine(linestring[8]);
                        writer.WriteLine("From: " + conf_mailsender);
                        writer.WriteLine(linestring[10]);
                        writer.WriteLine("Reply-To: " + conf_mailsender);
                        writer.WriteLine("To: " + conf_rcpt);
                        writer.WriteLine("Subject: " + conf_username_and_password);
                        writer.WriteLine("Return-Path: " + conf_mailsender);
                        writer.WriteLine(linestring[15]);
                        writer.WriteLine();

                        //Seperates start of email from the rest and decode parameter_content
                        string parameter_to = parserObject.getReciever(linestring[12]);
                        string parameter_content = parserObject.DecodeFrom64(linestring[17]);

                        //Creates string ready for base64 encode
                        string encode = "from=" + conf_sender + "&to=" + parameter_to + "&raport=" + conf_raport + "&message=" + parameter_content;

                        //Writes encoded string into buffer
                        writer.WriteLine(parserObject.EncodeTo64(encode));

                        //Writes buffer into .eml file
                        writer.Flush();
                        fs.Flush();

                        fs = null;
                        writer = null;
                        lines = null;
                    }
                        catch (System.IO.IOException e)
                    {
                        Console.WriteLine("no");

                    }

                    //Deletes the file
                    File.Delete(listfiles[j].FullName);
                }

                //Sets the number of files needing sent to 0
                listfiles = null;
            }
        }
    }
}

程序.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {           
        //Starts the Service
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new System.ServiceProcess.ServiceBase[]
            { new emlService() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

public class ParserCommands
{
    /// <summary>
    /// Seperates the address before @ from an email address
    /// </summary>
    /// <param name="email"> the email to be split </param>
    /// <returns> everything before @ on an email address </returns>
    public string getReciever(string email)
    {
        string[] Split = email.Split(new Char[] { '@' });
        string Split2 = Split[0];
        char[] chars = { 'T', 'o', ':', ' ' };
        string FinalSplit = Split2.TrimStart(chars);

        return FinalSplit;

    }

    /// <summary>
    /// Decodes base64 into string
    /// </summary>
    /// <param name="encodedData"> the base64 encoded data</param>
    /// <returns>decoded data as string</returns>
    public string DecodeFrom64(string encodedData)
    {
        //Turns into bytes and then turns bytes into string
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);

        return returnValue;
    }

    /// <summary>
    /// Decodes base64 data
    /// </summary>
    /// <param name="toEncode"> data that nees to be decoded</param>
    /// <returns> encoded data as string</returns>
    public string EncodeTo64(string toEncode)
    {
        //Turns string into bytes and then encodes it
        byte[] toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);
        string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);

        return returnValue;
    }
}

【问题讨论】:

  • 你在哪里关闭 fswriter
  • 尝试只发布损坏的代码的相关部分。当我们必须处理大量无关代码时,很难提供帮助
  • 对不起,Jason,我不知道到底是什么相关的,我对这一切都很陌生。

标签: c# file filestream streamwriter filelock


【解决方案1】:

FileStreamStreamWriterIDisposable,因此您应该将它们包装在 using 语句中,以确保它们在您完成后关闭。

using (FileStream fs = new FileStream(destpath + listfiles[j].Name, FileMode.CreateNew))
using (StreamWriter writer = new StreamWriter(fs)) {
   // .. your writing code here
}

【讨论】:

    【解决方案2】:

    来自StreamWriter 文档:

    一个好的做法是在 using 语句中使用这些对象,以便 非托管资源已正确处置。使用语句 当正在使用的代码自动调用对象上的 Dispose 它已经完成了。

    它们是 IDisposable,因此应包裹在 using 中。 Dispose 也应该调用 close。

    【讨论】:

      【解决方案3】:

      你有没有在写完东西后关闭文件流?

      【讨论】:

        猜你喜欢
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2014-11-06
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        相关资源
        最近更新 更多