Click here to Skip to main content
15,909,566 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'If Not Directory.Exists("C:\webapp\New Folder") Then
        Directory.CreateDirectory("C:\webapp\New Folder")
        ' Folder created message
        MessageBox.Show("Folder created!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        'Else
        '' Folder already exists
        'MessageBox.Show("Folder already exists!", "", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        'End If

        XMLFiles()

    End Sub


create XMLs and add them in new folder
VB
Public Sub XMLFiles()
        Dim userID As String = "d"
        Dim fname As String = "ll"
        Dim lname As String = "tt"
        Dim subject As String = " subject"
        Dim message As String = "This message"


        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        Using writer As XmlWriter = XmlWriter.Create("C:\webapp\New Folder\Info.xml", settings)
            writer.WriteStartDocument()
            writer.WriteStartElement("Info")
            writer.WriteStartElement("person")
            writer.WriteElementString("uid", userID)
            writer.WriteElementString("firstname", fname)
            writer.WriteElementString("lastname", lname)
            writer.WriteEndElement()
        End Using

        Using writer As XmlWriter = XmlWriter.Create("C:\webapp\New Folder\emailInfo.xml", settings)
            writer.WriteStartDocument()
            writer.WriteStartElement("emailInfo")
            writer.WriteElementString("subject", subject)
            writer.WriteElementString("message", message)
            writer.WriteEndElement()
        End Using
    End Sub
Posted
Updated 6-Sep-13 7:58am
v3
Comments
kj15 6-Sep-13 11:56am    
i am new, and i found this easy way around, but is there any better way to do this??

1 solution

There are no cases when hard-coding of any file path can be useful. The file paths should always be calculated during runtime, based on system configuration fetched from API, configuration files, user input, etc. In the case of a Web application, it is especially important: it is executed in a sandboxed environment which does not allow access to any file system objects which are not under the root directory reserved for the site.

Therefore, the hard-coded path you used may or may not be legitimate. Worse, such code is not supportable: should you change your hosting or any hosting environment detail, it will break your application. Moreover, if you use some shared hosting service, as most do, you won't have any control over the root directory of your site. The hosting provider may change it at any moment.

So, this is what you should fix. You can use Server.MapPath:
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx[^].

—SA
 
Share this answer
 
v2
Comments
kj15 6-Sep-13 12:45pm    
Thats great!! i appreciate your efforts in explainin it. thankyou so much!!
Sergey Alexandrovich Kryukov 6-Sep-13 21:21pm    
You are very welcome.
—SA
kj15 6-Sep-13 15:03pm    
so what path should i put for XML?
in this line..
Using writer As XmlWriter = XmlWriter.Create("........\userInfo.xml", settings) //what should go in there?
Sergey Alexandrovich Kryukov 6-Sep-13 21:23pm    
Is your concern a file name/pathname?
If so: you should have some directory known relative to the root directory set up on the server host for your site. You should create an absolute path from the result of MapPath.
—SA
kj15 9-Sep-13 12:52pm    
yes i got it!! my concern was pathname.
i used asp.net folder app_data and put the xml files in there.
since i had to send them to external server, so i just called the path:

Dim pathToFilesXMl1 = Server.MapPath("/App_Data") + "\UserInfo.xml"
Dim pathToFilesXML2 = Server.MapPath("/App_Data") + "\emailInfo.xml"


this worked, and i was able to get files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900