Solved

Get VS2013 to release a text file

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

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

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Export import database 4 43
Split in Javascript 5 31
Master Content Pages, WebForms, and Session Variables 1 34
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

13 Experts available now in Live!

Get 1:1 Help Now