程序無法存取檔案,因為檔案正由另一個程序使用

今天搞文件操作時,犯了一個很低級的錯誤,記下下來備忘。

今天做了一個文件的寫操作
1.創建文件
2.打開
3.寫
4.關閉

我想一次創建多個文件,就用了一個循環。可是老是出現下面錯誤:(圖:error_filecreate)
#--------------------------------------------------------------------------------
# An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
#
# Additional information: 由於另一個處理序正在使用檔案 "E:\temp\Admin.vb",所以無法存取該檔案。
#
#如圖:error_filecreate.jpg
#--------------------------------------------------------------------------------

我試了只創建一個文件,接著打開文件。結果:
#--------------------------------------------------------------------------------
#
#程序無法存取檔案,因為檔案正由另一個程序使用
#
#如圖:error_CreateThenOpen.jpg
#--------------------------------------------------------------------------------

我創建文件的代碼:
    Public Sub WriteLog(ByVal fullPath As String, ByVal content As String)
        If System.IO.Directory.Exists(GetDirectoryPath()) = False Then
            System.IO.Directory.CreateDirectory(GetDirectoryPath())
        Else
            If File.Exists(fullPath) = False Then
                'Dim _fs As FileStream = File.Create(fullPath)
                '_fs.Close()
                File.Create(fullPath)
            End If
        End If
        'Dim fs As New FileStream(fullPath, FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fullPath, True, System.Text.Encoding.GetEncoding("gb2312"))
        sw.WriteLine(content)
        sw.Flush()
        sw.Close()
        sw = Nothing
    End Sub


我猜想可能是文件指針或句柄什麽的一直指向創建的第一個文件,我想close,沒有找到File.Close()方法。
百度一下,發現有人這些創建文件:
                FileStream fs = File.Create(datafile);//创建xml文件
                fs.Close();
                StreamWriter sw = new StreamWriter(datafile, true, System.Text.Encoding.GetEncoding("gb2312"));
上面代碼使用了FileStream,FileStream提供Close,總算可以Close了。
最后使用上面的方法搞定了。

翻翻MSDN,文件創建例子如下:
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                ' Note that no lock is put on the
                ' file and the possibliity exists
                ' that another process could do
                ' something with it between
                ' the calls to Exists and Delete.
                File.Delete(path)
            End If
            ' Create the file.
            Dim fs As FileStream = File.Create(path)
            Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

            ' Add some information to the file.
            fs.Write(info, 0, info.Length)
            fs.Close()

            ' Open the stream and read it back.
            Dim sr As StreamReader = File.OpenText(path)
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch ex As Exception
            Console.WriteLine(ex)      
        End Try
    End Sub
End Class
參照MSDN,規範的寫代碼準沒錯!

posted @ 2008-06-27 19:38  yellowwood  阅读(10372)  评论(0编辑  收藏  举报
Never Give UP