Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

copy file from \\network share to c:\... VB.NET 2003\2010 Express

Posted on 2011-03-25
8
1,094 Views
Last Modified: 2012-06-21
Hi Experts,

I want to build a winapp which will list files in a listbox… if the listbox-item is selected and button1 clicked, it will copy/transfer the document from the network repository/folder to local machine (maybe a progress bar to show progress??????? optional).

1.      User opens App
2.      Listbox1 displays e.g. 9 (RTF) files.
3.      user selects Listbox1 item e.g. Outlook
4.      clicks Button1
5.      Outlook - something.rtf copies from network share to c:\dir

The reason I’ve chosen this is because stream-reading RTF docs from a network location can take time to view.

I have code to streamread into Listbox from DIR (which i will add to call, once I get chance).
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            FileCopy("C:\Documents and Settings\Bob\My Documents\test.txt", "C:\temp")

        Catch ex As Exception
            MsgBox("ok")
        End Try
       
    End Sub

Open in new window

0
Comment
Question by:RobertoFreemano
  • 5
  • 3
8 Comments
 

Author Comment

by:RobertoFreemano
ID: 35221465
If I don't add the TRY code, then I get the following error:

---error--------------------------------------------------------------------------------------------
An unhandled exception of type 'System.IO.IOException' occurred in microsoft.visualbasic.dll

Additional information: The target directory already exists.
------------------------------------------------------------------------------------------------------
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35222058
Are you asking for something more than My.Computer.FileSystem.CopyFile, which has an option for a progress bar, ala Windows Explorer?

File Copy Progress, Custom Thread Pools
http://msdn.microsoft.com/en-us/magazine/cc163851.aspx
0
 

Author Comment

by:RobertoFreemano
ID: 35235154
Sorry TheLearnedOne,

I fond a solution which works but I was wondering if it could incorporate a progress bar?

Thanks,
Roberto
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If

        Dim di2 As System.IO.DirectoryInfo

        path = "c:\temp\temp2"
        di2 = New System.IO.DirectoryInfo(path)

        If (Not di2.Exists) Then
            di2.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inputStream, outputStream As FileStream
        Dim fileSize As Long
        Dim ipFilebyte As Byte
        inputStream = File.OpenRead("c:\temp\test.txt") 'server path
        fileSize = inputStream.Length()

        outputStream = File.OpenWrite("c:\temp\temp2\test.txt") 'local path

        While fileSize
            ipFilebyte = inputStream.ReadByte()
            outputStream.WriteByte(ipFilebyte)
            fileSize -= 1
        End While

        outputStream.Close()
        inputStream.Close()

    End Sub

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 35235271
1) You don't need to use a FileStream to get a progress bar, you can use the FileSystem.CopyFile to copy with a progress bar in 1 line.

2) If you insist on the FileStream approach, then I would suggest using a BackgroundWorker, with progress enabled, and the ReportProgress.
0
 

Author Comment

by:RobertoFreemano
ID: 35235308
Thanks TheLearnedOne,

I'm clueless when it comes to practical coding :(

I wonder if you could assist with "FileSystem.CopyFile "
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35237580
That would be something like this:

      My.Computer.FileSystem.CopyFile(sourceFileName, destinationFileName, FileIO.UIOption.AllDialogs)
0
 

Author Comment

by:RobertoFreemano
ID: 35314307
Hi TLO,

I'll test this now ;)

Sorry about the delay :(
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35386623
Thanks ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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