Declaration & " This Process cannot access...." errors in VB.NET

I've a form(developed using WindowsApplication) asking START DATE, END DATE & MESSAGE(which appears on each Windows login in betwen these dates). If Windows login occurs after the END DATE, then 3 days 'll get added. I'm writing all the form values to a separate TXT file called "Dates.txt". For this purpose the code is as & it works properly

Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If CDateChecker.WriteFile("c:\Dates.txt", dtmStart.Value, dtmEnd.Value, txtMessage.Text) Then
            Me.Close()
        Else
            MessageBox.Show("Could not write the file")
        End If
    End Sub
End Class

Public Class CDateChecker
    Private Const DATEFORMAT = "dd/MMM/yyyy"

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As String, ByVal dtmEnd As String, ByVal Message As String) As Boolean
        Return WriteFile(FileName, Convert.ToDateTime(dtmStart), Convert.ToDateTime(dtmEnd), Message)
    End Function

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamWriter = Nothing

        Try
            sw = New StreamWriter(FileName, False)
            sw.WriteLine(dtmStart.ToString(DATEFORMAT))
            sw.WriteLine(dtmEnd.ToString(DATEFORMAT))
            sw.WriteLine(Message)
            Return True
        Catch ex As Exception
            MessageBox.Show("ex.Message")
            Return False
        Finally
            If Not sw Is Nothing Then sw.Close()
        End Try
    End Function
End Class

The values r now safely written 2 the TXT file. Now I've 2 retrive those. I entered the following code using "ConsoleApplication" & "WindowsApplication" separately. But on runtime, the ConsoleApplication return me the error "Name 'MessageBox' is not declared". Then I tried the same code in WindowsApplication which on run time gave errors like

1st ERROR - "The process cannot access the file 'c:\Dates.txt' because it is being used by another process"

2nd ERROR - "An unhandled error has occurred in your application"

What's the solution for both these errors? So plz tell me what change in code shall b made to remove the above mentioned errors in both application types(i.e. Console & windows appl.)? The code 4 2nd file is as;

CONSOLE APPLICATION CODE
--------------------------------------------
Imports System.IO

Module Module1

    Sub Main()
        Dim dtmStart As Date
        Dim dtmEnd As Date
        Dim sMessage As String = String.Empty

        If CDateChecker.ReadFile("c:\Dates.txt", dtmStart, dtmEnd, sMessage) Then
            If Now >= dtmStart AndAlso Now <= dtmEnd Then
                MessageBox.Show(sMessage)
            Else
                dtmEnd = dtmEnd.AddDays(3)
                CDateChecker.WriteFile("c:\Dates.txt", dtmStart, dtmEnd, sMessage)
            End If
        End If
    End Sub

End Module

Public Class CDateChecker
    Private Const DATEFORMAT = "dd/MMM/yyyy"

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As String, ByVal dtmEnd As String, ByVal Message As String) As Boolean
        Return WriteFile(FileName, Convert.ToDateTime(dtmStart), Convert.ToDateTime(dtmEnd), Message)
    End Function

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamWriter = Nothing

        Try
            sw = New StreamWriter(FileName, False)
            sw.WriteLine(dtmStart.ToString(DATEFORMAT))
            sw.WriteLine(dtmEnd.ToString(DATEFORMAT))
            sw.WriteLine(Message)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        Finally
            If Not sw Is Nothing Then sw.Close()
        End Try
    End Function

    Public Shared Function ReadFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamReader = Nothing

        If Not File.Exists(FileName) Then Return False

        Try
            sw = New StreamReader(FileName)
            dtmStart = Convert.ToDateTime(sw.ReadLine())
            dtmEnd = Convert.ToDateTime(sw.ReadLine())
            Message = sw.ReadLine()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If sw Is Nothing Then sw.Close()
        End Try
    End Function
End Class

-------------------------------------------
WINDOWS APPLICATION CODE
--------------------------------------------
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Close()
        Dim dtmStart As Date
        Dim dtmEnd As Date
        Dim sMessage As String = String.Empty

        If CDateChecker.ReadFile("c:\Dates.txt", dtmStart, dtmEnd, sMessage) Then
            If Now >= dtmStart AndAlso Now <= dtmEnd Then
                MessageBox.Show(sMessage)
            Else
                dtmEnd = dtmEnd.AddDays(3)
                CDateChecker.WriteFile("c:\Dates.txt", dtmStart, dtmEnd, sMessage)
            End If
        End If
    End Sub
End Class

Public Class CDateChecker
    Private Const DATEFORMAT = "dd/MMM/yyyy"

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As String, ByVal dtmEnd As String, ByVal Message As String) As Boolean
        Return WriteFile(FileName, Convert.ToDateTime(dtmStart), Convert.ToDateTime(dtmEnd), Message)
    End Function

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamWriter = Nothing

        Try
            sw = New StreamWriter(FileName, False)
            sw.WriteLine(dtmStart.ToString(DATEFORMAT))
            sw.WriteLine(dtmEnd.ToString(DATEFORMAT))
            sw.WriteLine(Message)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        Finally
            If sw Is Nothing Then sw.Close()
        End Try
    End Function

    Public Shared Function ReadFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamReader = Nothing

        Try
            sw = New StreamReader(FileName)
            dtmStart = Convert.ToDateTime(sw.ReadLine())
            dtmEnd = Convert.ToDateTime(sw.ReadLine())
            Message = sw.ReadLine()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        Finally
            If sw Is Nothing Then sw.Close()
        End Try
    End Function
End Class

I must tell u about the function of this code. It simply reads the START DATE, END DATE & MESSAGE from the "Dates.txt" file & displays a Msgbox(containing MESSAGE) in between the START & END DATES.

I hope u got understood.... But plz reply me back....
eljosephAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Well the solution to the first is that you need to Import the System.Windows.Forms namepace into your console app in order to use the MessageBox.
0
eljosephAuthor Commented:
ok lemme try it then

Thank God somebody replied so far
0
eljosephAuthor Commented:
I received the following error on adding this import

"Namespace or type 'Forms' for the Imports 'System.Windows.Forms' cannot be found."

Whats its solution then?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Carl TawnSystems and Integration DeveloperCommented:
Go to Project -> Add Reference and include the System.Windows.Forms.dll
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eljosephAuthor Commented:
ok after addinng the DLL file's reference I run the program. It runs but returns with this error

"This process cannot access the "c:\Dates.txt" because it is being used by another process"

Wht 2 do then?
0
Carl TawnSystems and Integration DeveloperCommented:
Have you got anything else running that is using the file ?
0
eljosephAuthor Commented:
Yeah the portion of the code which writes the form inputs to TXT file. The above mentioned code is included in a separate file 2 retrieve the inputs
0
Carl TawnSystems and Integration DeveloperCommented:
You mean you have two apps, both your own that are accessing the file at the same time ?
0
eljosephAuthor Commented:
But the first file is not opened in Visual Studio at the moment, only 2nd one
0
eljosephAuthor Commented:
Its right that I've developed 2 applications but they dun access at the same time.

The first one simply inputs & the second one runs on every Windows login
0
Carl TawnSystems and Integration DeveloperCommented:
Does the logon program release the handle to the file correctly and exit properly when its finished ? If not then it may be retaining a handle to the file.
0
eljosephAuthor Commented:
Well look when I tried it till morning 2day, it gives me Msgbox with a particular msg & immediately afterwards the form appears once again to input something

But I dun enter once more & just close it manually
0
eljosephAuthor Commented:
Sorry forgot to write u something....... Till morning almost every action was linked to just 1 file, the once placed in the START UP folder.

Just in the afternoon I split the code into 2 separate files & removed the old one from START UP
0
eljosephAuthor Commented:
CODE OF FIRST FILE 'FORM1.VB'
---------------------------------------------------
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If CDateChecker.WriteFile("c:\Dates.txt", dtmStart.Value, dtmEnd.Value, txtMessage.Text) Then
            Me.Close()
        Else
            MessageBox.Show("Could not write the file")
        End If
    End Sub
End Class

Public Class CDateChecker
    Private Const DATEFORMAT = "dd/MMM/yyyy"

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As String, ByVal dtmEnd As String, ByVal Message As String) As Boolean
        Return WriteFile(FileName, Convert.ToDateTime(dtmStart), Convert.ToDateTime(dtmEnd), Message)
    End Function

    Public Shared Function WriteFile(ByVal FileName As String, ByVal dtmStart As Date, ByVal dtmEnd As Date, ByVal Message As String) As Boolean
        Dim sw As StreamWriter = Nothing

        Try
            sw = New StreamWriter(FileName, False)
            sw.WriteLine(dtmStart.ToString(DATEFORMAT))
            sw.WriteLine(dtmEnd.ToString(DATEFORMAT))
            sw.WriteLine(Message)
            Return True
        Catch ex As Exception
            MessageBox.Show("ex.Message")
            Return False
        Finally
            If Not sw Is Nothing Then sw.Close()
        End Try
    End Function
End Class



CODE OF SECOND FILE MODULE1.VB'
---------------------------------------------------
Imports System.IO
Module Module1

    Sub Main()
        ' Create an instance of StreamWriter to write text to a file.
        Dim sw As StreamWriter = New StreamWriter("c:\Khan.txt")
            ' Add some text to the file.
            sw.Write("This is the ")
            sw.WriteLine("header for the file.")
            sw.WriteLine("-------------------")
            ' Arbitrary objects can also be written to the file.
            sw.Write("The date is: ")
            sw.WriteLine(DateTime.Now)
            sw.Close()
   
    Dim MyMessage As String = ""
        MyMessage &= "Computer Name: " & Environment.MachineName & vbCrLf
        MyMessage &= "Windows Name: " & Environment.OSVersion.Platform.ToString & " v" & Environment.OSVersion.Version.ToString & vbCrLf
        MyMessage &= "User Name: " & Environment.UserDomainName & "\" & Environment.UserName & vbCrLf
        MyMessage &= "Computer Started: " & DateTime.Now.AddMilliseconds(Environment.TickCount * -1).ToString
        MsgBox(MyMessage)
    End Sub

End Module
0
eljosephAuthor Commented:
Ok its running freely now.....

Ok just check the code I posted at first in this question. When I change the system date(greater than the END DATE written in the file), 3 days r not being added. Why?

Is there anyway if the Console DOS Windows doesn't appear on every Windows login? That truly gives a bad impact
0
eljosephAuthor Commented:
Go to Project -> Add Reference

This is something very new u told me carl_tawn

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.