यह फ़ंक्शन निर्धारित करता है कि 'लेखन' मोड में रुचि की फ़ाइल तक पहुंचा जा सकता है या नहीं। यह एक प्रक्रिया द्वारा लॉक किया गया है या नहीं यह निर्धारित करने के समान बिल्कुल नहीं है। फिर भी, आप पाते हैं कि यह आपकी स्थिति के लिए काम करता है। (कम से कम जब तक कुछ बेहतर साथ आता है।)
इस समारोह का संकेत देगा कि 'लिखने की पहुँच जब एक फ़ाइल अन्य प्रक्रिया द्वारा अवरोधित किया गया है संभव नहीं है। हालांकि, यह उस स्थिति को अन्य स्थितियों से अलग नहीं कर सकता जो 'लेखन' पहुंच को रोकती है। उदाहरण के लिए, 'लिखना' पहुंच भी संभव नहीं है यदि किसी फ़ाइल में केवल पढ़ने के लिए बिट सेट है या इसमें प्रतिबंधित एनटीएफएस अनुमतियां हैं। जब इन 'लेखन' पहुंच प्रयास किए जाते हैं तो इन सभी स्थितियों के परिणामस्वरूप 'अनुमति अस्वीकृत' होगी।
भी ध्यान रखें कि यदि फ़ाइल को अन्य प्रक्रिया द्वारा अवरोधित किया गया है, इस सवाल का जवाब इस समारोह से वापस लौटे ही पल समारोह निष्पादित किया जाता है पर विश्वसनीय है। तो, समवर्ती समस्याएं संभव हैं।
एक अपवाद अगर इन शर्तों के किसी भी पाए जाते हैं फेंक दिया जाता है: 'फ़ाइल नहीं मिला', 'पथ नहीं मिला', या 'अवैध फ़ाइल नाम' ('बुरा फ़ाइल नाम या नंबर')।
Function IsWriteAccessible(sFilePath)
' Strategy: Attempt to open the specified file in 'append' mode.
' Does not appear to change the 'modified' date on the file.
' Works with binary files as well as text files.
' Only 'ForAppending' is needed here. Define these constants
' outside of this function if you need them elsewhere in
' your source file.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
IsWriteAccessible = False
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Dim nErr : nErr = 0
Dim sDesc : sDesc = ""
Dim oFile : Set oFile = oFso.OpenTextFile(sFilePath, ForAppending)
If Err.Number = 0 Then
oFile.Close
If Err Then
nErr = Err.Number
sDesc = Err.Description
Else
IsWriteAccessible = True
End if
Else
Select Case Err.Number
Case 70
' Permission denied because:
' - file is open by another process
' - read-only bit is set on file, *or*
' - NTFS Access Control List settings (ACLs) on file
' prevents access
Case Else
' 52 - Bad file name or number
' 53 - File not found
' 76 - Path not found
nErr = Err.Number
sDesc = Err.Description
End Select
End If
' The following two statements are superfluous. The VB6 garbage
' collector will free 'oFile' and 'oFso' when this function completes
' and they go out of scope. See Eric Lippert's article for more:
' http://blogs.msdn.com/b/ericlippert/archive/2004/04/28/when-are-you-required-to-set-objects-to-nothing.aspx
'Set oFile = Nothing
'Set oFso = Nothing
On Error GoTo 0
If nErr Then
Err.Raise nErr, , sDesc
End If
End Function
डैरिन नोट (अन्य जवाब में) है कि इस मॉड्यूल शामिल करना चाहिए: 'कॉन्स्ट ForReading = 1, ForWriting = 2, ForAppending = 8' – Smandoli
@Smandoli - मेरा ध्यान करने के लिए इस चूक लाने के लिए धन्यवाद। मैंने तदनुसार कोड अपडेट किया है। यह भी ध्यान रखें मेरी टिप्पणी समारोह के अंत में स्थापित करने 'Nothing' को' oFile' और 'oFso' पूर्ववर्ती। – DavidRR