Hello all!
i am a beginner to network programming. I am trying to make an application in vb.net 2008 where in i can open a file from the client computer, do some changes to it and save it back. Can someone please throw some light on how should i go ahead with it and what will be the best way to do the same.
Thanks you!

I am trying to make an application in vb.net 2008 where in i can open a file from the client computer, do some changes to it and save it back.

There's no difference between network files and local files in the way you open and modify them. That may be a bit simplified statement but basically that's how it goes.

Here's a snippet that uses a OpenFileDialog to select a (text) file, reads the file's content and displays the content in a mulltiline textbox

Dim FileName As String              ' File's name
Dim oStream As FileStream           ' Using System.IO
Dim fStreamReader As StreamReader   ' Using System.IO (could use My.Computer.FileSystem.ReadAllText())
Dim FileText As String              ' Read a text file

OpenFileDialog1.AddExtension = False
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = ""
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "All Files (*.*)|*.*"
OpenFileDialog1.InitialDirectory = ""
OpenFileDialog1.Multiselect = False
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.SupportMultiDottedExtensions = True
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    FileName = OpenFileDialog1.FileName
    Try
        FileText = ""
        oStream = New FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
        fStreamReader = New StreamReader(oStream)

        Do Until fStreamReader.EndOfStream
            FileText = FileText & fStreamReader.ReadLine() & Environment.NewLine
        Loop
        TextBox1.Multiline = True
        TextBox1.Text = FileText
        ' Clean up
        fStreamReader.Close()
        oStream.Close()
        fStreamReader = Nothing
        oStream = Nothing
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error")
    End Try

End If

If you see and you can select a file from the OpenFileDialog, you most likely can read and modify that file too. If you don't have enough rights to access a file, it makes no difference if it's network or local file.

HTH

hi Teme64,
i guess u got it wrong or actually it was my mistake only, i didn't frame the question properly. What you have told me is opening a file which exist their in same system. My scenario is i have 2 computers say comp1 and comp2. Now i want to read a file in comp1 that actually exist their in comp2. How to do the this?

And he answered your question.
By using the OpenFileDialog, you can browse the network to the other computer, select the file and open it. Network or Local, it makes no difference.

Or, if you know the path and the file you want to read is the same, over and over again. You can hard-code the path to the file via an UNC path: \\comp2\folder\filename.ext

oh ya . This is 1 method. But this will be a module for my network's project. I dont want to use directly a dialog box and implement it. I want to implement a network protocol. I did some study and read about Sockets. Will it be actually possible using Sockets?

I was assuming, like Oxiegen did, that you talk about computers in a same local area network. Now that you mentioned Sockets, I started to wonder if you're trying to open/modify over internet? Is that what you mean?

That's also possible, but then we're talking about a totally different scenario. And you're right about Sockets. You can establish a connection to the remote computer with a socket, transfer a file to your local computer, edit the file and finally upload it back with a socket. If you're familiar with googling, search for file transfer with sockets and VB.NET. You should find sample codes how to do the file transfer. Editing the file is the trivial part :)

HTH

This article has been dead for over six months. Start a new discussion instead.