Environment versions
Network:
Windows 2008 Server
>Hello everyone;
>
>I have a function which makes a merge TXT files ; this go through different folders and taking txt files in each folder is then join the files.
>
>The issue that I have is that after the function makes scanning all folders , the function generates the following eror :
>
>An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
>Additional information: The process cannot access the file 'C:\Users\liderapp\Documents\Scripts\Octubre2015\CM.TXT' because it is being used by another process.
>
>The function is:
>
> For Each topDir As DirectoryInfo In New DirectoryInfo(sourceDir).GetDirectories("*", SearchOption.TopDirectoryOnly)
> For Each subDir As DirectoryInfo In topDir.GetDirectories("*", SearchOption.AllDirectories)
> For Each txtfile As FileInfo In topDir.GetFiles("*.txt", SearchOption.AllDirectories)
> If txtfile.Name.Equals(NameCM, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameCM
> ElseIf txtfile.Name.Equals(NameGL, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameGL
> ElseIf txtfile.Name.Equals(NameImp60, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameImp60
> ElseIf txtfile.Name.Equals(NameImp61, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameImp61
> ElseIf txtfile.Name.Equals(NameImp62, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameImp62
> ElseIf txtfile.Name.Equals(NameImp63, StringComparison.OrdinalIgnoreCase) Then
> curFilename = NameImp63
> Else
> curFilename = String.Empty
> End If
> If Not String.IsNullOrEmpty(curFilename) Then
> Debug.WriteLine(topDir.FullName)
> Debug.WriteLine(subDir.FullName)
> Debug.WriteLine(txtfile.FullName)
> Debug.WriteLine(Path.Combine(topDir.FullName, curFilename))
> Using sr As StreamReader = txtfile.OpenText
> File.AppendAllText(Path.Combine(topDir.FullName, curFilename), sr.ReadToEnd, Encoding.Default)
> End Using
> End If
> Next txtfile
> Next subDir
> Next topDir
>
>The issue happen when the scaning process finish and occur in this sentences:
>File.AppendAllText(Path.Combine(topDir.FullName, curFilename), sr.ReadToEnd, Encoding.Default)
>
>How I can avoid the error ?
>
>TIA
Not clear by what you mean by 'The issue happen when the scaning process finish' - the line which you say generates the error is within the loop.
Also both File.AppendAllText and StreamReader.ReadToEnd can throw the System.IO.Exception - is 'C:\Users\liderapp\Documents\Scripts\Octubre2015\CM.TXT' the source or destination file ?
Previous
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only