N
news.iq.ca
Hi. I am trying to use a treeview - after a few attempts, it works, but only
up to a point - my exercise was to dynamically create nodes in the treeview.
I read about binding the treeview to a XML file. Okay, so I decided to
complicate my exercise and to create the XML file on the fly. I am using
this code to browse through the list of my other projects, create the XML
file and then populate the treeview. I am not using the XML writer object
because I did not reach that point with my learning, so I am writing it "the
old way":
<code>
Private m_strPath As String = "C:\Documents and
Settings\Administrator\My Documents\My
Projects\ASPNETProjects\vsnet\ThirdPartyControls\TreeNodes.XML"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
CreateXMLFile()
TreeView1.TreeNodeSrc = m_strPath
TreeView1.DataBind()
End If
End Sub
Private Sub CreateXMLFile()
Dim objDirectory As Directory
Dim ProjectInfo As FileInfo
Dim lstFiles As String()
Dim strProjectLongName As String
Dim strProjectShortName As String
Dim strmFile As StreamWriter
Dim objStringBuilder As New StringBuilder()
Const PROJECTS_PATH As String = "C:\Documents and
Settings\Administrator\My Documents\My Projects\ASPNETProjects\notepad\"
strmFile = File.CreateText(m_strPath)
strmFile.WriteLine("<TREENODES>")
strmFile.WriteLine("<TREENODE TEXT=""ASP.NET PROJECTS"">")
lstFiles = objDirectory.GetFiles(PROJECTS_PATH, "*.aspx")
For Each strProjectLongName In lstFiles
ProjectInfo = New FileInfo(strProjectLongName)
strProjectShortName = ProjectInfo.Name
objStringBuilder = New StringBuilder()
objStringBuilder.Append("<TREENODE TEXT=""" &
strProjectShortName & """ NAVIGATEURL =
""HTTP:\\LOCALHOST\ASPNETPROJECTS\NOTEPAD\")
objStringBuilder.Append(strProjectShortName)
objStringBuilder.Append("""" & "/>")
strmFile.WriteLine(objStringBuilder.ToString)
Next strProjectLongName
strmFile.WriteLine("</TREENODE>")
strmFile.Write("</TREENODES>")
strmFile.Flush()
strmFile.Close()
strmFile = Nothing
objStringBuilder = Nothing
lstFiles = Nothing
objDirectory = Nothing
End Sub
</code>
This code works fine, but ... if I run the application one more time, I get:
________________________________________________
Exception Details: System.IO.IOException: The process cannot access the file
"C:\Documents and Settings\Administrator\My Documents\My
Projects\ASPNETProjects\vsnet\ThirdPartyControls\TreeNodes.XML" because it
is being used by another process.
________________________________________________
I also discovered that I can't even delete the "TreeNodes.XML" file IN
EXPLORER (!) because the file is locked.. VERY STRANGE - this happens EVEN
if I close VS.NET !
I discovered that the only way out is to kill the process "aspnet_wp" in
task manager - it gets recreated instantly, even with VS.NET closed (!?),
but at least I can now delete the file in Explorer. So the sequence to make
it work is this: run the app the first time - stop it - kill the aspnet_wp
process (it gets recreated) - now I can run the app again without having the
above-mentioned error.
What's with this lock ??? Am I doing something wrong in this code ? I even
set to nothing almost all objects in my app, except the strings, even if
this is useless....
Thank you.
Alex.
up to a point - my exercise was to dynamically create nodes in the treeview.
I read about binding the treeview to a XML file. Okay, so I decided to
complicate my exercise and to create the XML file on the fly. I am using
this code to browse through the list of my other projects, create the XML
file and then populate the treeview. I am not using the XML writer object
because I did not reach that point with my learning, so I am writing it "the
old way":
<code>
Private m_strPath As String = "C:\Documents and
Settings\Administrator\My Documents\My
Projects\ASPNETProjects\vsnet\ThirdPartyControls\TreeNodes.XML"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
CreateXMLFile()
TreeView1.TreeNodeSrc = m_strPath
TreeView1.DataBind()
End If
End Sub
Private Sub CreateXMLFile()
Dim objDirectory As Directory
Dim ProjectInfo As FileInfo
Dim lstFiles As String()
Dim strProjectLongName As String
Dim strProjectShortName As String
Dim strmFile As StreamWriter
Dim objStringBuilder As New StringBuilder()
Const PROJECTS_PATH As String = "C:\Documents and
Settings\Administrator\My Documents\My Projects\ASPNETProjects\notepad\"
strmFile = File.CreateText(m_strPath)
strmFile.WriteLine("<TREENODES>")
strmFile.WriteLine("<TREENODE TEXT=""ASP.NET PROJECTS"">")
lstFiles = objDirectory.GetFiles(PROJECTS_PATH, "*.aspx")
For Each strProjectLongName In lstFiles
ProjectInfo = New FileInfo(strProjectLongName)
strProjectShortName = ProjectInfo.Name
objStringBuilder = New StringBuilder()
objStringBuilder.Append("<TREENODE TEXT=""" &
strProjectShortName & """ NAVIGATEURL =
""HTTP:\\LOCALHOST\ASPNETPROJECTS\NOTEPAD\")
objStringBuilder.Append(strProjectShortName)
objStringBuilder.Append("""" & "/>")
strmFile.WriteLine(objStringBuilder.ToString)
Next strProjectLongName
strmFile.WriteLine("</TREENODE>")
strmFile.Write("</TREENODES>")
strmFile.Flush()
strmFile.Close()
strmFile = Nothing
objStringBuilder = Nothing
lstFiles = Nothing
objDirectory = Nothing
End Sub
</code>
This code works fine, but ... if I run the application one more time, I get:
________________________________________________
Exception Details: System.IO.IOException: The process cannot access the file
"C:\Documents and Settings\Administrator\My Documents\My
Projects\ASPNETProjects\vsnet\ThirdPartyControls\TreeNodes.XML" because it
is being used by another process.
________________________________________________
I also discovered that I can't even delete the "TreeNodes.XML" file IN
EXPLORER (!) because the file is locked.. VERY STRANGE - this happens EVEN
if I close VS.NET !
I discovered that the only way out is to kill the process "aspnet_wp" in
task manager - it gets recreated instantly, even with VS.NET closed (!?),
but at least I can now delete the file in Explorer. So the sequence to make
it work is this: run the app the first time - stop it - kill the aspnet_wp
process (it gets recreated) - now I can run the app again without having the
above-mentioned error.
What's with this lock ??? Am I doing something wrong in this code ? I even
set to nothing almost all objects in my app, except the strings, even if
this is useless....
Thank you.
Alex.