12 Replies - 620 Views - Last Post: 17 August 2009 - 11:49 AM Rate Topic: -----

#1 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

File Writing Error

Posted 16 August 2009 - 12:57 PM

Hello,

i try to write some data in a ini file. i can do that first time but second i give me an error: System.IO.IOException was unhandled
Message="The process cannot access the file "

here is my code

Imports System.IO
Module mdlMainBegin


	Public Structure FInfo
		Dim InitPage As String
		Dim HistoryFilPath As String
		Dim HistorySDate As String
	End Structure

	Public Structure HistoryInfo
		Dim LastUsedUrl As String
		Dim cntArry As Long
		Dim UsedUrl() As String
	End Structure


	Public FI As FInfo
	Public HI As HistoryInfo

	Sub Main()
		Dim formMainLoad As New frmMain
		Dim strPath As String
		'Dim ioLine As String ' Going to hold one line at a time
		'Dim ioLines As String ' Going to hold whole file

		strPath = Path.GetDirectoryName(Application.ExecutablePath)
		Debug.Print(strPath)
		If Dir(strPath & "\StartPage.ini") = "" Then
			MsgBox("Не е открит файл с ностройките. Ще бъде създаден такъв по подразбиране")
			With FI
				.HistoryFilPath = Path.GetDirectoryName(Application.ExecutablePath) & "\History"
				.InitPage = "http://www.google.com"
				.HistorySDate = Format(Now, "dd/mm/yyyy")
				If File.Exists(strPath & "\StartPage.ini") Then
				Else
					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters = Nothing
				End If
			End With
		End If

		If Dir(strPath & "\History\Hist.ini") = "" Then
			If Dir(strPath & "\History") = "" Then
				MkDir(strPath & "\History")
				strPath = strPath & "\History\Hist.ini"
			End If
			With HI
				.cntArry = 0
				.LastUsedUrl = ""
				ReDim .UsedUrl(.cntArry)
				.UsedUrl(.cntArry) = "http://www.google.com"
				If File.Exists(strPath & "\History\Hist.ini") Then

				Else
					File.Create(strPath)
					Dim fWriters = New StreamWriter(strPath)
					fWriters.WriteLine(.LastUsedUrl)
					For i = 0 To .cntArry
						fWriters.WriteLine(.UsedUrl(.cntArry))
					Next
				End If

			End With
		End If

		Application.Run(formMainLoad)
	End Sub
End Module




can some one tell me why is this happening :)

ty in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: File Writing Error

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 146
  • View blog
  • Posts: 1,567
  • Joined: 16-December 08

Re: File Writing Error

Posted 16 August 2009 - 03:12 PM

View PostNoBrain, on 16 Aug, 2009 - 12:57 PM, said:

Hello,

i try to write some data in a ini file. i can do that first time but second i give me an error: System.IO.IOException was unhandled
Message="The process cannot access the file "

here is my code

Imports System.IO
Module mdlMainBegin


	Public Structure FInfo
		Dim InitPage As String
		Dim HistoryFilPath As String
		Dim HistorySDate As String
	End Structure

	Public Structure HistoryInfo
		Dim LastUsedUrl As String
		Dim cntArry As Long
		Dim UsedUrl() As String
	End Structure


	Public FI As FInfo
	Public HI As HistoryInfo

	Sub Main()
		Dim formMainLoad As New frmMain
		Dim strPath As String
		'Dim ioLine As String ' Going to hold one line at a time
		'Dim ioLines As String ' Going to hold whole file

		strPath = Path.GetDirectoryName(Application.ExecutablePath)
		Debug.Print(strPath)
		If Dir(strPath & "\StartPage.ini") = "" Then
			MsgBox("Не е открит файл с ностройките. Ще бъде създаден такъв по подразбиране")
			With FI
				.HistoryFilPath = Path.GetDirectoryName(Application.ExecutablePath) & "\History"
				.InitPage = "http://www.google.com"
				.HistorySDate = Format(Now, "dd/mm/yyyy")
				If File.Exists(strPath & "\StartPage.ini") Then
				Else
					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters = Nothing
				End If
			End With
		End If

		If Dir(strPath & "\History\Hist.ini") = "" Then
			If Dir(strPath & "\History") = "" Then
				MkDir(strPath & "\History")
				strPath = strPath & "\History\Hist.ini"
			End If
			With HI
				.cntArry = 0
				.LastUsedUrl = ""
				ReDim .UsedUrl(.cntArry)
				.UsedUrl(.cntArry) = "http://www.google.com"
				If File.Exists(strPath & "\History\Hist.ini") Then

				Else
					File.Create(strPath)
					Dim fWriters = New StreamWriter(strPath)
					fWriters.WriteLine(.LastUsedUrl)
					For i = 0 To .cntArry
						fWriters.WriteLine(.UsedUrl(.cntArry))
					Next
				End If

			End With
		End If

		Application.Run(formMainLoad)
	End Sub
End Module




can some one tell me why is this happening :)

ty in advance :)

It may be cause of the file being "already opened", it seems like you never called close on the streamwriters.
Was This Post Helpful? 0
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 09:28 AM

hello dude ty for replay :)
well i close it i do this

   Sub Main()
		Dim formMainLoad As New frmMain
		Dim strPath As String
		'Dim ioLine As String ' Going to hold one line at a time
		'Dim ioLines As String ' Going to hold whole file

		strPath = Path.GetDirectoryName(Application.ExecutablePath)
		Debug.Print(strPath)
		If Dir(strPath & "\StartPage.ini") = "" Then
			MsgBox("Can not find options files.Crating ...", MsgBoxStyle.OkOnly)
			With FI
				.HistoryFilPath = Path.GetDirectoryName(Application.ExecutablePath) & "\History"
				.InitPage = "http://www.google.com"
				.HistorySDate = Format(Now, "dd/mm/yyyy")
				If File.Exists(strPath & "\StartPage.ini") Then
				Else
					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters.Close()
					fWriters = Nothing
				End If
			End With
		End If

		If Dir(strPath & "\History\Hist.ini") = "" Then
			If Dir(strPath & "\History") = "" Then
				MkDir(strPath & "\History")
				strPath = strPath & "\History\Hist.ini"
			End If
			With HI
				.cntArry = 0
				.LastUsedUrl = ""
				ReDim .UsedUrl(.cntArry)
				.UsedUrl(.cntArry) = "http://www.google.com"
				If File.Exists(strPath & "\History\Hist.ini") Then

				Else
					File.Create(strPath)
					Dim fWritersss = New StreamWriter(strPath)
					fWritersss.WriteLine(.LastUsedUrl)
					For i = 0 To .cntArry
						fWritersss.WriteLine(.UsedUrl(.cntArry))
					Next
				End If

			End With
		End If

		Application.Run(formMainLoad)
	End Sub



and it give me an error on this row

Dim fWritersss = New StreamWriter(strPath) ' strPath is application path 



and the error is : "The process cannot access the file" and strPath(c:\test\History\Hist.ini)

so the files are created but it cannot access it for somer reason. :)
can you help me pls
ty for your time :)

EDIT: i close the streamWriter and change me second and still no luck

This post has been edited by NoBrain: 17 August 2009 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: File Writing Error

Posted 17 August 2009 - 09:32 AM

try adding a try catch block to work out the problem I think it is that when you create the files you don't close the stream writter as previously mentioned this means that it cannot be accessed by any program.
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 09:36 AM

View PostAsscotte, on 17 Aug, 2009 - 08:32 AM, said:

try adding a try catch block to work out the problem I think it is that when you create the files you don't close the stream writter as previously mentioned this means that it cannot be accessed by any program.

hi dude ty for your fast replay

i did close it

					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters.Close()
					fWriters = Nothing




i will try "try" :)

EDIT : pls can you show me how to use "try" catch pls :)

This post has been edited by NoBrain: 17 August 2009 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 146
  • View blog
  • Posts: 1,567
  • Joined: 16-December 08

Re: File Writing Error

Posted 17 August 2009 - 11:05 AM

View PostNoBrain, on 17 Aug, 2009 - 09:36 AM, said:

View PostAsscotte, on 17 Aug, 2009 - 08:32 AM, said:

try adding a try catch block to work out the problem I think it is that when you create the files you don't close the stream writter as previously mentioned this means that it cannot be accessed by any program.

hi dude ty for your fast replay

i did close it

					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters.Close()
					fWriters = Nothing




i will try "try" :)

EDIT : pls can you show me how to use "try" catch pls :)

Try
					File.Create(strPath & "\StartPaga.ini")
					Dim fWriters As New StreamWriter(strPath & "\StartPage.ini")
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
					fWriters.Close()
					fWriters = Nothing
catch ex as exception
MsgBox(ex.message) '<---if the code above gets a error, it will show a message box
end try



Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 11:08 AM

well dude ty for showing me how to youse try catch but the error is the same :)

The process cannot access the file" and strPath(c:\test\History\Hist.ini)

This post has been edited by NoBrain: 17 August 2009 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: File Writing Error

Posted 17 August 2009 - 11:26 AM

First, you have so much code that is legacy libraries from the VB6 days, such as Dir and MsgBox, if you're going to work with VB.NET you should really stick with the native .Net libraries.

Now for your issues, I would start using the Using Statement, this takes care of opening and closing (then disposing) of your objects for you. Here's a small modification to your code using this. I also do not use File.Create, I the StreamWriter and append True for appending (this will append the text if the file exists, if the file doesnt exist it creates it and adds the text)

If Not File.Exists(strPath & "\StartPaga.ini") Then
	Using writer As New StreamWriter(strPath & "\StartPage.ini", True)
		writer.WriteLine("1|" & .HistoryFilPath)
		writer.WriteLine("2|" & .HistoryFilPath)
		writer.WriteLine("3|" & .HistorySDate)
	End Using
End If



That should get you started down the right path to fixing your problem :)
Was This Post Helpful? 1
  • +
  • -

#9 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 11:33 AM

ty :)

can i ask you what name is in this line

Quote

Using writer As New StreamWriter(name)

ty again for your help :)

EDIT : NVM

This post has been edited by NoBrain: 17 August 2009 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: File Writing Error

Posted 17 August 2009 - 11:35 AM

Refresh and look, I edited the post as you were reading it. I initially started making an example procedure then changed my mind.
Was This Post Helpful? 0
  • +
  • -

#11 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 11:38 AM

yes i see it ty :)
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: File Writing Error

Posted 17 August 2009 - 11:45 AM

Here's your code with no legacy functions in it

Sub Main()
	Dim formMainLoad As New frmMain
	Dim strPath As String
	'Dim ioLine As String ' Going to hold one line at a time
	'Dim ioLines As String ' Going to hold whole file

	strPath = Path.GetDirectoryName(Application.ExecutablePath)
	Debug.Print(strPath)
	If Not File.Exists(strPath & "\StartPage.ini") Then
		MessageBox.Show("Не е открит файл с ностройките. Ще бъде създаден такъв по подразбиране")
		With FI
			.HistoryFilPath = Path.GetDirectoryName(Application.ExecutablePath) & "\History"
			.InitPage = "http://www.google.com"
			.HistorySDate = Format(Now, "dd/mm/yyyy")
			If Not File.Exists(strPath & "\StartPage.ini") Then
				Using fWriters As New StreamWriter(strPath & "\StartPage.ini", True)
					fWriters.WriteLine("1|" & .HistoryFilPath)
					fWriters.WriteLine("2|" & .HistoryFilPath)
					fWriters.WriteLine("3|" & .HistorySDate)
				End Using
			End If
		End With
	End If

	If Not File.Exists(strPath & "\History\Hist.ini") Then
		If Not Directory.Exists(strPath & "\History") Then
			Directory.CreateDirectory(strPath & "\History")
			strPath = strPath & "\History\Hist.ini"
		End If
		With HI
			.cntArry = 0
			.LastUsedUrl = ""
			ReDim .UsedUrl(.cntArry)
			.UsedUrl(.cntArry) = "http://www.google.com"
			Using fWriters As New StreamWriter(strPath, True)
				fWriters.WriteLine(.LastUsedUrl)
				For i = 0 To .cntArry
					fWriters.WriteLine(.UsedUrl(.cntArry))
				Next
			End Using
		End With
	End If

	Application.Run(formMainLoad)
End Sub


Was This Post Helpful? 0
  • +
  • -

#13 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: File Writing Error

Posted 17 August 2009 - 11:49 AM

ty so very much :) i can tell you how much i am greatfull to you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1