Solved

Get VS2013 to release a text file

Posted on 2014-10-30
3
160 Views
Last Modified: 2014-10-31
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 rtn 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:\1015_AMPTK_205_BIZ_DEV_TARG\3100_Support\TestVar.txt"
        Dim varWriter As StreamWriter
        Dim wrkFile As FileStream
        Try
            wrkFile = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
        Catch
            Return "$ERR$ReadTestVarFile$26797541$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
3 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 350 total points
ID: 40414934
Typically it means that other program or your site thread is using this file. to avoid that make sure you do not open the file in  Notepad (or other editor). If it still fails the modify your cod as follows:
1) Replace line 13 with
wrkFile = New FileStream(varFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)

Open in new window

2) Close the stream (insert at line 33 for quick test - but you should do it as a try finally block)
wrkFile.Close()
0
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 150 total points
ID: 40414988
you are never closing the file you need to close it if you want to write to it with another application.

End While
        '--------------------------------
        wrkFile.Close()
        wrkFileReader = Nothing
        wrkFile = Nothing

Open in new window

0
 
LVL 2

Author Closing Comment

by:codequest
ID: 40416134
Thanks!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

775 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