Ecrire dans un fichier INI avec ASP

cs_fredb23 Messages postés 2 Date d'inscription jeudi 11 mars 2004 Statut Membre Dernière intervention 11 mars 2004 - 11 mars 2004 à 14:01
cs_fredb23 Messages postés 2 Date d'inscription jeudi 11 mars 2004 Statut Membre Dernière intervention 11 mars 2004 - 11 mars 2004 à 16:40
Bonjour,
J'ai récuperé le code suivant sur le web :

<%
Sub WriteINIString(Section, KeyName, Value, FileName)
Dim INIContents, PosSection, PosEndSection

'Get contents of the INI file As a string
INIContents = GetFile(FileName)

'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection>0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section? If PosEndSection 0 Then PosEndSection Len(INIContents)+1

'Separate section contents
Dim OldsContents, NewsContents, Line
Dim sKeyName, Found
OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
OldsContents = split(OldsContents, vbCrLf)

'Temp variable To find a Key
sKeyName = LCase(KeyName & "=")

'Enumerate section lines
For Each Line In OldsContents
If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
Line = KeyName & "=" & Value
Found = True
End If
NewsContents = NewsContents & Line & vbCrLf
Next

If isempty(Found) Then
'key Not found - add it at the end of section
NewsContents = NewsContents & KeyName & "=" & Value
Else
'remove last vbCrLf - the vbCrLf is at PosEndSection
NewsContents = Left(NewsContents, Len(NewsContents) - 2)
End If

'Combine pre-section, new section And post-section data.
INIContents = Left(INIContents, PosSection-1) & _
NewsContents & Mid(INIContents, PosEndSection)
else'if PosSection>0 Then
'Section Not found. Add section data at the end of file contents.
If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
INIContents = INIContents & vbCrLf
End If
INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
KeyName & "=" & Value
end if'if PosSection>0 Then
WriteFile FileName, INIContents
End Sub

'File functions
Function GetFile(ByVal FileName)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left (FileName,2)<>"\" Then
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If
On Error Resume Next

GetFile = FS.OpenTextFile(FileName).ReadAll
End Function

Function WriteFile(ByVal FileName, ByVal Contents)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left (FileName,2)<>"\" Then
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If
Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function

WriteINIString "Identification", "msg", "hello world", "E:\Email\EMAIL.INI"
%>

Voila le message que me retourne le navigateur :

Erreur d'exécution Microsoft VBScript error '800a0046'

Permission refusée

/test_intra/untitled2.asp, line 76

je test en local sur ma machine et j'ai bien les droits d'ecriture sur le fichier EMAIL.INI

Quel peut etre le probleme ??

merci beaucoup

1 réponse

cs_fredb23 Messages postés 2 Date d'inscription jeudi 11 mars 2004 Statut Membre Dernière intervention 11 mars 2004
11 mars 2004 à 16:40
Ok ça fonctionne très bien, c'était bien un simple problème de droits sur le fichier.

Donc ce code fonctionne au poil, pour ceux qui veulent gerer les fichiers .ini en ASP sans ajouter un enième composant.
0
Rejoignez-nous