我正在关闭并处置本地声明的流编写器,但我仍然得到另一个进程异常的文件使用

发布于 2024-12-22 09:57:42 字数 2001 浏览 0 评论 0原文

我试图实现几件事:

  1. 将一堆单独的文本文件合并到一个组合文件中,
  2. 将组合的文件移动到一个新文件夹中。

我已将这些任务中的每一个分配给一个方法。

即使我正在关闭并处置流读取器,第一种方法中的某些内容仍保留在文件上 - 但我不知道是什么。我知道第一种方法的进程挂在文件上,因为当我注释掉第一种方法时,第二种方法可以毫无问题地移动文件。

编辑 第一种方法是返回新大文件的值。这作为参数传递给第二个方法。当第二种方法尝试引用此文件时,即程序抛出错误时(从底部开始第 5 行)

SO 上的大多数帖子建议关闭/处置 IO 对象,但我已经这样做了。他们还建议使用第三方工具来调查哪个进程挂在文件上,但我已经知道这是第一个合并方法(因为当它被注释掉时程序会运行)。

这是一个类似问题的示例: https://stackoverflow.com/questions/8088225/file- use-by-another-process-exception-in-c-sharp

我错过了什么?

newBigFile as string = mergeFiles (inputLocation, outputLocation) 
moveFiles = moveMergedFiles(inputLocation, outputLocation, newBigFile)

Private Function mergeFiles(ByVal inputLocationFilesToMerge As String, ByVal outputLocationHL7 As String) As String


    Dim newMergedFile As String = inputLocationFilesToMerge & "\jointFile" & DateTime.Now.Month & DateTime.Now.Day & DateTime.Now.Year & DateTime.Now.Hour & DateTime.Now.Minute & ".hl7"
    Dim objWriter As New System.IO.StreamWriter(newMergedFile)
    Dim HL7FilePaths As String() = System.IO.Directory.GetFiles(inputLocationFilesToMerge, "*.hl7")

    For Each p As String In HL7FilePaths
        Dim sr As StreamReader = New StreamReader(p)
        objWriter.Write(sr)
        sr.Close()
        sr.Dispose()
    Next

    objWriter.Close()
    objWriter.Dispose()

    Return (newMergedFile) 

End Function

Private Sub moveMergedFiles(ByVal inputLocationFilesToMerge As String, ByVal outputLocationHL7 As String, ByVal mergedFile As String)


    For Each q As String In System.IO.Directory.GetFiles(inputLocationFilesToMerge, "*.hl7")
        If (Not (q = mergedFile)) Then **this is where the error comes from**
            File.Move(q, outputLocationHL7 & "\" & Path.GetFileName(q)) 
        End If
    Next
End Sub

Am trying to achieve a couple of things:

  1. merge a bunch of individual text files into a combined file,
  2. move the files that were combined into a new folder.

I have assigned each of these tasks to a method.

Something in the first method is hanging on to the file, even though I am closing and disposing the stream reader - but I can't figure out what. I know that a process from the first method is hanging on to the file because when I comment out the first method, the second method moves the files without a problem.

EDIT The first method is returning a value for new big file. This is passed as a parameter to the second method. When the second method tries to reference this file, that is when the program throws the error (5th line from bottom)

Most posts on SO advise closing/disposing IO objects, but I am already doing that. They also advise using a third party tool to investigate what process is hanging on to a file, but I already know that it is the first merge method (because when it is commented out the program runs).

This is an example similar question:
https://stackoverflow.com/questions/8088225/file-use-by-another-process-exception-in-c-sharp

What am I missing?

newBigFile as string = mergeFiles (inputLocation, outputLocation) 
moveFiles = moveMergedFiles(inputLocation, outputLocation, newBigFile)

Private Function mergeFiles(ByVal inputLocationFilesToMerge As String, ByVal outputLocationHL7 As String) As String


    Dim newMergedFile As String = inputLocationFilesToMerge & "\jointFile" & DateTime.Now.Month & DateTime.Now.Day & DateTime.Now.Year & DateTime.Now.Hour & DateTime.Now.Minute & ".hl7"
    Dim objWriter As New System.IO.StreamWriter(newMergedFile)
    Dim HL7FilePaths As String() = System.IO.Directory.GetFiles(inputLocationFilesToMerge, "*.hl7")

    For Each p As String In HL7FilePaths
        Dim sr As StreamReader = New StreamReader(p)
        objWriter.Write(sr)
        sr.Close()
        sr.Dispose()
    Next

    objWriter.Close()
    objWriter.Dispose()

    Return (newMergedFile) 

End Function

Private Sub moveMergedFiles(ByVal inputLocationFilesToMerge As String, ByVal outputLocationHL7 As String, ByVal mergedFile As String)


    For Each q As String In System.IO.Directory.GetFiles(inputLocationFilesToMerge, "*.hl7")
        If (Not (q = mergedFile)) Then **this is where the error comes from**
            File.Move(q, outputLocationHL7 & "\" & Path.GetFileName(q)) 
        End If
    Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淤浪 2024-12-29 09:57:42

objWriter.Write(sr) 行上,您不应该调用 sr.ReadToEnd() 吗?似乎您正在传递streamReader 对象而不是流数据(除非存在我不知道的隐式转换)。

如果内存不是问题,也许可以尝试用 StreamReader 代替快速的 File.ReadAllBytes(filename)。至少这样你可以知道它是否是streamReader。

On the line objWriter.Write(sr) shouldn't you call sr.ReadToEnd()? Seems like you're passing the streamReader object rather than the stream data (unless there is an implicit cast that I am unaware of).

If memory is not a concern, maybe try substituting the StreamReader for a quick File.ReadAllBytes(filename). At least this way you can see if it is the streamReader or not.

北陌 2024-12-29 09:57:42
Dim objWriter As New System.IO.StreamWriter(newMergedFile)

代码中的其他地方还使用了这个? - 通常这是因为您创建了太多新实例和/或该文件的流仍然打开,只需创建 objWriter As New System.IO.StreamWriter(newMergedFile) 的全局实例,

然后完成后用 objWriter.Close() 关闭它,我退出这个。代码看起来很混乱,缺乏清晰的思想和方向。

Dim objWriter As New System.IO.StreamWriter(newMergedFile)

Where else in code is this being used? - Usually this is caused because you are creating too many new Instances and or the Stream of that file is still open just create a Global instance of objWriter As New System.IO.StreamWriter(newMergedFile)

Then when done Close that with objWriter.Close() I am backing out of this one.. code looks way to messing and lacks clear thought and direction..

醉梦枕江山 2024-12-29 09:57:42

你没有处置 objwriter。
我还建议您对物理文件使用 FileStream

,最后但并非最不重要的是

在 C# 中使用 So(ish),因为我不是 VB 头

using (FileStream fs = new FileStream(newMergedFile, FileMode.Create, FileAccess.Write))
{
    TextWriter objWriter = new StreamWriter(fs);
    foreach(String fileName in HL7FilePaths)
    {
        using (FileStream frs = new FileStream(fileName,FileMode.Open, FileAccess.Read))
        {
            ObjWriter.AppendStream(frs);
            ObjWriter.Flush(); 
        }
    }
}

不记得新 FileStream、创建和合并的 ReadWrite 的确切语法文件和打开并读取文件模式,文件访问或类似的东西通常可以完成这项工作。

只是我们养成的习惯,始终使用 using 块,它会为您关闭并处理,Flush() 来确保,如果在您尝试读取或写入之前出现任何问题,FileStream 将无法尝试获取文件。

如果您遵循这样的路径,则很少会出现文件问题。

You aren't disposing of objwriter.
I'd also suggest you use FileStream for physical files

and last but far from least important using

So in C# (ish) as I'm not a VB head

using (FileStream fs = new FileStream(newMergedFile, FileMode.Create, FileAccess.Write))
{
    TextWriter objWriter = new StreamWriter(fs);
    foreach(String fileName in HL7FilePaths)
    {
        using (FileStream frs = new FileStream(fileName,FileMode.Open, FileAccess.Read))
        {
            ObjWriter.AppendStream(frs);
            ObjWriter.Flush(); 
        }
    }
}

Can't remember the exact syntax for new FileStream, Create and ReadWrite for the merged file and Open and read for FileMode, and FileAccess or somesuch usually do the job.

Just a habit we've developed, always use a using block, that will close and dispose for you, Flush() to make sure, and FileStream will fail trying to get the file if there's anything wrong well before you try to read or write.

Rarely get file problems, if you follow something like this path.

音盲 2024-12-29 09:57:42

两个问题...
1)当正在使用jointFile进行写入时,您试图打开该jointFile进行读取。
2)您需要使用 sr.ReadToEnd 读取文件,如 ach

Change:

For Each p As String In HL7FilePaths      
     Dim sr As StreamReader = New StreamReader(p)   
     objWriter.Write(sr)           
     sr.Close()
     sr.Dispose()       
Next   

To:

For Each p As String In HL7FilePaths
            If p <> newMergedFile Then
                Dim sr As StreamReader = New StreamReader(p)
                objWriter.Write(sr.ReadToEnd)
                sr.Close()
                sr.Dispose()
            End If
Next

Two problems...
1) You are trying to open the jointFile for reading when it is being used to write to.
2) You need to read the file with a sr.ReadToEnd as stated by ach

Change:

For Each p As String In HL7FilePaths      
     Dim sr As StreamReader = New StreamReader(p)   
     objWriter.Write(sr)           
     sr.Close()
     sr.Dispose()       
Next   

To:

For Each p As String In HL7FilePaths
            If p <> newMergedFile Then
                Dim sr As StreamReader = New StreamReader(p)
                objWriter.Write(sr.ReadToEnd)
                sr.Close()
                sr.Dispose()
            End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文