?
Solved

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
3
Medium Priority
?
319 Views
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.)
0
Comment
Question by:thenthorn1010
  • 2
3 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 35709469
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
          io.Directory.CreateDirectory(fi.directoryname)
endif


0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35709560
Hi, you can try this:
Dim path As String = String.Empty
' Connect to your DB
' More connection string info at http://connectionstrings.com/sql-server-2005
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
    cn.Open()
    ' 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
    System.IO.Directory.CreateDirectory(path)
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

0
 
LVL 17

Expert Comment

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 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