Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-26
16
Medium Priority
?
227 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....
0
Comment
Question by:eljoseph
  • 11
  • 5
16 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16293125
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
 

Author Comment

by:eljoseph
ID: 16293184
ok lemme try it then

Thank God somebody replied so far
0
 

Author Comment

by:eljoseph
ID: 16293190
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 80 total points
ID: 16293196
Go to Project -> Add Reference and include the System.Windows.Forms.dll
0
 

Author Comment

by:eljoseph
ID: 16293225
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16293227
Have you got anything else running that is using the file ?
0
 

Author Comment

by:eljoseph
ID: 16293241
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16293243
You mean you have two apps, both your own that are accessing the file at the same time ?
0
 

Author Comment

by:eljoseph
ID: 16293249
But the first file is not opened in Visual Studio at the moment, only 2nd one
0
 

Author Comment

by:eljoseph
ID: 16293259
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16293268
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
 

Author Comment

by:eljoseph
ID: 16293283
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
 

Author Comment

by:eljoseph
ID: 16293290
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
 

Author Comment

by:eljoseph
ID: 16293417
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
 

Author Comment

by:eljoseph
ID: 16293671
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
 

Author Comment

by:eljoseph
ID: 16308343
Go to Project -> Add Reference

This is something very new u told me carl_tawn

Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

580 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