3 Replies - 2038 Views - Last Post: 19 June 2011 - 05:40 PM Rate Topic: -----

#1 deva.v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-June 11

Save File Dialog and Writing in text file

Posted 09 June 2011 - 01:28 AM

Hi,

I am creating a text file using a save file dialog and copying the path to the text box which file is used to write from vb.net. when i try to write i get an error "The process cannot access the file '....fileName.txt' because it is being used by other program". How to resolve this

The code is below

    Public Sub writeInTextFile()
    Try
    Dim oWrite As New StreamWriter(TxtOutputPath.Text)
    Dim colnCt, rowCt As Integer
    colnCt = GridDataOutField.Columns.Count
    rowCt = GridDataOutField.Rows.Count
    Dim gridData As String = ""
     
    For i As Integer = 0 To colnCt - 1
    If i = 0 Then
    gridData = GridDataOutField.Columns(i).Name
    Else
    gridData += vbTab + GridDataOutField.Columns(i).Name
    End If
     
    Next
    oWrite.WriteLine(gridData)
     
    For i As Integer = 0 To rowCt - 2
    gridData = ""
    For j As Integer = 0 To colnCt - 1
    If j = 0 Then
    gridData = GridDataOutField.Item(j, i).Value
    Else
    gridData += vbTab + GridDataOutField.Item(j, i).Value
    End If
    Next
    oWrite.WriteLine(gridData)
    Next
    MsgBox("Sucessfully written to the selected path")
    oWrite.Close()
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Save File Dialog and Writing in text file

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 150
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Save File Dialog and Writing in text file

Posted 09 June 2011 - 06:10 AM

First, you have to figure what out is using your file: Another instance of your program; Windows deleting the file; Etc?

If you can't find another program that's using the file, handle it with a try-catch statement. Then, save to another file to see if it works.
Was This Post Helpful? 0
  • +
  • -

#3 samuel17  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 209
  • Joined: 14-October 10

Re: Save File Dialog and Writing in text file

Posted 18 June 2011 - 07:02 PM

Hi deva.v,
Id check your task manager processes, or just tasks to verify that you dont have the text file open somewhere else or to see if it didnt fully close and is running in the background. Any chance you can past all of your code. Also where is the file located? You noted using the save file dialog, so im guessing the file could be located anywhere, not just on the application start location. I just wonder if its how you are accessing the file maybe? Let us know how you are coming on this. Any updates?

Sam
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Save File Dialog and Writing in text file

Posted 19 June 2011 - 05:40 PM

As was stated make sure your program isn't still running in the processes, because earlier attempts could be your problem. Are you disposing your objects somewhere else? If you don't dispose properly this may happen. :) I like to use using blocks for disposing, helps keep the code clean.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1