Solved

Get VS2013 to release a text file

Posted on 2014-10-30
3
157 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 78

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

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

19 Experts available now in Live!

Get 1:1 Help Now