How to Write to A Directory From Visual Basic, First Making the Directory Path, Then Adding the file Name to a File Writer

Posted on 2011-05-06
Last Modified: 2012-05-11
I am attempting to write data to a file from a parameter that is stored within a SQL Server 2005 database. (The value of the entire file path can be retrieved by a query similar to "Select EDIOutputPath From SystemParms;.") I need to figure out how to first take the value returned by the query, which will be a string type, check to make sure that the directory listed exists. If the directory listed does not exist, I need to be able to create the directory. (For example, if the above query returns the output location "C:\test\EDI\", I need to be able to test if that location exists. If it exists then you move onto writing data to a file named abc.txt, for simplicity of testing. If the path does not exist, then the VB .NET 2010 code needs to create the directory before writing to the output file named "abc.txt") Any help that you would be able to provide with this task would be greatly appreciated. Also, how would I be able to test whether or not the location that is being written to is a "Shared" resource on a network where others are able to access the same folder via a server or a cluster of workstations that are on a network? (This would be done using VB .NET 2010 code.)
Question by:thenthorn1010
    LVL 18

    Expert Comment

    Here's the check for the directory and if not found the create
    dim FI as new io.fileinfo(your path)
    IF not IO.Directory.exists (fi.directoryname) then

    LVL 17

    Accepted Solution

    Hi, you can try this:
    Dim path As String = String.Empty
    ' Connect to your DB
    ' More connection string info at
    Using cn As New System.Data.SqlClient.SqlConnection("MyConnectionString")
        Dim cm As New System.Data.SqlClient.SqlCommand("Select TOP 1 EDIOutputPath From SystemParms", cn)
        ' Open your connection
        ' Retrieve the value of your query (First row, first column)
        path = Convert.ToString(cm.ExecuteScalar())
    End Using
    ' Check if directory exist
    If Not System.IO.Directory.Exists(path) Then
        ' Create directory
    End If
    ' Create the file (file contents optional)
    System.IO.File.WriteAllText(System.IO.Path.Combine(path, "abc.txt"), "")
    End Sub

    Open in new window

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Oops, remove the "End Sub" at line 20.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now