Copying Files

Hi Everyone,

I needed a little help with copying a certain file type from one folder into another. For instance copying all the .dll files from one folder into another . All help is appreciated

I know i can use the System.IO.File.Copy to do it but how do i specify just a certain file type not a file



Answer this question

Copying Files

  • Aqua General

    Here is an example to do that (to copy DLLs files only form direcotry to another), however, it's a Visual Basic .NET code :
    Dim FileFullPath As String
    For Each FileFullPath In System.IO.Directory.GetFiles("DirPath")
    If System.IO.Path.GetExtension(FileFullPath).ToLower = ".dll" Then
    System.IO.File.Copy(FileFullPath, "C:\Test\" & System.IO.Path.GetFileName(FileFullPath), True)
    End If
    Next

    Hope this helps you.


  • mcdonaldn

    Copy Method:

    public static void CopyDir2Dir(string sourceDir, string desDir, string searchPattern)

    {

    DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);

    foreach (FileInfo file in dirInfo.GetFiles(searchPattern))

    {

    file.CopyTo(desDir + "\\" + file.Name);

    }

    }

    Usage:

    CopyDir2Dir(@"C:\dir1", @"C:\dir2", "*.dll");

    this will copy all dlls from dir1 to dir2.



  • akin_l

    Agent00 wrote:

    Hi,

    Does anybody know why I can't copy/move files from "Temporary Internet Files", but I can for other folder

    Thanks in advance

    Jason

    This thread was marked as complete, you might want to post your own thread with that question



  • Gunston

    Stefan Prodan wrote:

    Copy Method:

    public static void CopyDir2Dir(string sourceDir, string desDir, string searchPattern)

    {

    DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);

    foreach (FileInfo file in dirInfo.GetFiles(searchPattern))

    {

    file.CopyTo(desDir + "\\" + file.Name);

    }

    }

    Usage:

    CopyDir2Dir(@"C:\dir1", @"C:\dir2", "*.dll");

    this will copy all dlls from dir1 to dir2.

    Once again man thanks your a life saver


  • HugoC

    Use System.IO.Directory.GetFiles(), then check the extension, if the type is what you want to copy, then copy it using System.IO.File.Copy().
    Hope this helps a bit.


  • GrantWestCoast

    Hi,

    Does anybody know why I can't copy/move files from "Temporary Internet Files", but I can for other folder

    Thanks in advance

    Jason



  • Stevenmja

    Could you help me out a little with the could an example perhaps im a novice at the .file namespace thank you much
  • MKBender

    lol uhhhhh doesnt help because it is in vb.net but isnt there a way in VS05 to convert the code Because im a newbie and dont know how to do it
  • Copying Files