Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
TXT file doesn´t close
Message
From
21/10/2015 04:42:41
 
 
To
20/10/2015 11:28:24
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB.NET 1.1
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01626227
Message ID:
01626255
Views:
62
>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
Map
View

Click here to load this message in the networking platform