Solved

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

Posted on 2011-03-25
8
1,097 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

732 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