4 Replies - 445 Views - Last Post: 15 July 2011 - 12:42 PM Rate Topic: -----

#1 kiranvolety  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 11

Close a text file which is opened and write to it

Posted 15 July 2011 - 09:19 AM

Hi,

I am trying to write text to a file in this path "C:\Test\test.txt" and when I want to save image filenames to this path I need to check if the file is opened and if it is opened I need to close the ".txt" file and write the text to the text file.

I tried everything but nothing worked for me and on the web everyone gave the same example.

Here is my code:

 Using sfdlg As New Windows.Forms.SaveFileDialog
            sfdlg.DefaultExt = "amk"
            sfdlg.Filter = "AquaMark Project|*.amk"
            If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then


                Dim SaveData As New gCanvasData

                Dim myfile As String = "C:\Test\test.txt"
                If System.IO.File.Exists(myfile) = True Then
                    'Delete it!

                    Dim fi As New FileInfo(myfile)
                    fi.Delete()
                End If


                IO.Directory.CreateDirectory("C:\Test")
                Dim w As New IO.StreamWriter("C:\Test\test.txt")
                Dim i As Integer


                For i = 0 To CheckedListBox1.Items.Count - 1
                    w.WriteLine(CheckedListBox1.Items.Item(i))
                Next
                w.Close()
                With SaveData
                    frmDisplay.GCanvas1.UnselectCurrentAnotate()
                    .gAnnotates = frmDisplay.GCanvas1.gAnnotates
                    .Image = frmDisplay.GCanvas1.Image
                End With

                Using objStreamWriter As New StreamWriter(sfdlg.FileName)
                    Dim x As New XmlSerializer(GetType(gCanvasData))
                    x.Serialize(objStreamWriter, SaveData)
                    objStreamWriter.Close()
                End Using
            End If
        End Using



And I tried this but it didn't work for me:

Try
System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
FileClose(1)
Return False
Catch ex As Exception
Return True
End Try


Is This A Good Question/Topic? 0
  • +

Replies To: Close a text file which is opened and write to it

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Close a text file which is opened and write to it

Posted 15 July 2011 - 12:23 PM

I don't think it is an elegant way to determine if a file it is opened. When an application opens a file, it keeps a handle to that file. So, if you try to move the file or delete it, an exception will be raised that will point out that the file is used by another process. You may try this way.
Was This Post Helpful? 0
  • +
  • -

#3 kiranvolety  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 11

Re: Close a text file which is opened and write to it

Posted 15 July 2011 - 12:36 PM

Hi Ionut,

Thanks for your reply,and at last I found the solution and here is the code I have modified to get that:

  Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
        For Each p As Process In Process
            If p.MainWindowTitle.Contains("test") Then
                p.Kill()
            End If
        Next

Was This Post Helpful? 0
  • +
  • -

#4 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Close a text file which is opened and write to it

Posted 15 July 2011 - 12:38 PM

It is a solution, but what if there is an other application(not notepad or Word or Wordpad) that opens a file as you do?
Was This Post Helpful? 0
  • +
  • -

#5 kiranvolety  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 11

Re: Close a text file which is opened and write to it

Posted 15 July 2011 - 12:42 PM

Here is the way you can get it:

 Dim processID As Integer = 1040 'suppose 1040 is process id of the running file
       Dim p As Process = Process.GetProcessById(processID)
       p.Kill()
You can use following code to know whether file is running or not. 

 Public Function IsFileRunning(filename As String) As Boolean
        Try
            Dim fs As FileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
            fs.Close()
            Return False
        Catch ex As IOException
            Return True
        End Try
    End Function

This post has been edited by kiranvolety: 15 July 2011 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1