We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

eljoseph
eljoseph asked
on
Medium Priority
245 Views
Last Modified: 2010-04-23
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....
Comment
Watch Question

Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
ok lemme try it then

Thank God somebody replied so far

Author

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?
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
Go to Project -> Add Reference and include the System.Windows.Forms.dll

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Have you got anything else running that is using the file ?

Author

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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
You mean you have two apps, both your own that are accessing the file at the same time ?

Author

Commented:
But the first file is not opened in Visual Studio at the moment, only 2nd one

Author

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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.

Author

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

Author

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

Author

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

Author

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

Author

Commented:
Go to Project -> Add Reference

This is something very new u told me carl_tawn

Thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.