Link to home
Start Free TrialLog in
Avatar of eljoseph
eljoseph

asked on

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....
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of eljoseph
eljoseph

ASKER

ok lemme try it then

Thank God somebody replied so far
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?
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Have you got anything else running that is using the file ?
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
You mean you have two apps, both your own that are accessing the file at the same time ?
But the first file is not opened in Visual Studio at the moment, only 2nd one
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
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.
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
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
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
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
Go to Project -> Add Reference

This is something very new u told me carl_tawn

Thanks