使用c#并行写入文件

时间:2017-05-08 21:32:30

标签: c# concurrency io

我有一个同时写入文件的应用程序

当它运行到最后时,它应该是大约100k行。现在,我将在写完10k记录后收到消息。

以下是错误:

  

System.IO .__ Error.Void WinIOError(Int32,System.String) - 进程无法访问文件'C:\ Temp \ xxx.txt',因为它正由另一个进程使用。

代码:

public class FileExport
{
    private object lockPathObject = new object();
    private object lockACLbject = new object();
    static string pathHeader = "ScanDate,Path,IsDirectory,Files,Size,Owner,Accessed," +
        "Created,Modified,sourcetype";
    static string aclHeader = "ScanDate,Path,Trustee,Permissions,AccessType,Inherit," + 
        "SID,sourcetype";
    static string sourceType = "EUSFileservice";

    private static NamedReaderWriterLockSlim<string> locker = 
        new NamedReaderWriterLockSlim<string>();

    public void ExportPath(IEnumerable<PathInfo> paths, string filename)
    {
        try
        {
            // lock (lockPathObject)
            using (locker.LockWrite(filename))
            {
                using (StreamWriter w = File.AppendText(filename))
                {
                    if (w.BaseStream.Position == 0)
                        w.WriteLine(pathHeader);
                    foreach (PathInfo path in paths)
                    {
                        if (path == null)
                            continue;

                        w.WriteLine("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\"," + 
                            "\"{5}\",\"{6}\",\"{7}\",\"{8}\"", DateTime.UtcNow, 
                            path.Path, (path.IsDirectory ? 1 : 0), path.Files, 
                            path.Size, path.Owner, path.AccessDate, path.CreateDate, 
                            path.ModifiedDate, sourceType);
                    }

                    w.Flush();
                }
            }
        }
        catch(Exception ex)
        {
            ErrorLogger.LogEvent(ex);
        }
        return;
    }

我是从

调用的
void ProcessTasks()
{
    FileExport se = new FileExport();
    while (true)
    {
        var pathList = pathQueue.DequeueChunk(20);

        se.ExportPath(pathList, pathFileName);

        if (pathQueue.Count == 0)
        {
            break;
        }
    }
}

我尝试使用锁定NamedReaderWriterLockSlim并获得相同的结果。有什么建议吗?

更新 错误发生在:

using (StreamWriter w = File.AppendText(filename))

堆栈跟踪

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append)
   at System.IO.File.AppendText(String path)

0 个答案:

没有答案