Solved

Get VS2013 to release a text file

Posted on 2014-10-30
2
122 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
ID: 40414820
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
ID: 40414869
Sweet!  Many thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

17 Experts available now in Live!

Get 1:1 Help Now