Solved

Get VS2013 to release a text file

Posted on 2014-10-30
2
118 Views
Last Modified: 2014-10-30
In Win7 VS2013 asp.net, I want to use a text file to provide a variable to my web site, so that I can change the variable externally to the site during run time, to cause the site to run different code paths based on the variable.

But I get a "file in use" message when I try to update the text file.

Any suggestions on how to get around this, or to otherwise create the same effect, would be appreciated.

    Public Shared Function GetTestVar1(ByRef varVarName As String) As String
        Dim rtnVal As String = "not found"
        '-----------------------------------
        Dim p1 As Integer = 0
        Dim p2 As Integer = 0
        Dim l1 As Integer = 0
        Dim varFilePath As String
        varFilePath = "D:\mypath\TestVar.txt"
        Dim varWriter As StreamWriter
        Dim wrkFile As FileStream
        Try
            wrkFile = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
        Catch
            Return "$ERR$ReadTestVarFile$FilePath =" & varFilePath
            Exit Function
        End Try
        Dim wrkFileReader As StreamReader = New StreamReader(wrkFile)
        Dim n As Integer = 0
        Dim inLine As String
        '----------------------------Read
        While (wrkFileReader.Peek > -1)
            inLine = wrkFileReader.ReadLine
            If InStr(inLine, varVarName) <> 0 Then
                p1 = InStr(inLine, "=")
                If p1 <> 0 Then
                    l1 = Len(inLine)
                    rtnVal = Mid(inLine, p1 + 1, l1 - p1 + 1)
                End If
            End If
        End While
        '--------------------------------
        wrkFileReader = Nothing
        wrkFile = Nothing
        Return rtnVal
    End Function

Open in new window

0
Comment
Question by:codequest
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
You should use Using to close the StreamReader, like this:
    Public Shared Function GetTestVar1(ByRef varVarName As String) As String
        Dim rtnVal As String = "not found"
        '-----------------------------------
        Dim p1 As Integer = 0
        Dim p2 As Integer = 0
        Dim l1 As Integer = 0
        Dim varFilePath As String
        varFilePath = "D:\mypath\TestVar.txt"
        Dim varWriter As StreamWriter
        Dim wrkFile As FileStream
        Try
            wrkFile = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
        Catch
            Return "$ERR$ReadTestVarFile$FilePath =" & varFilePath
            Exit Function
        End Try
        Using wrkFileReader As StreamReader = New StreamReader(wrkFile)
        Dim n As Integer = 0
        Dim inLine As String
        '----------------------------Read
        While (wrkFileReader.Peek > -1)
            inLine = wrkFileReader.ReadLine
            If InStr(inLine, varVarName) <> 0 Then
                p1 = InStr(inLine, "=")
                If p1 <> 0 Then
                    l1 = Len(inLine)
                    rtnVal = Mid(inLine, p1 + 1, l1 - p1 + 1)
                End If
            End If
        End While
        '--------------------------------
        End Using
        wrkFile = Nothing
        Return rtnVal
    End Function
                                  

Open in new window

0
 
LVL 2

Author Comment

by:codequest
Comment Utility
Sweet!  Many thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

9 Experts available now in Live!

Get 1:1 Help Now