Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi dear all

Please tell me how to copy files from one folder to another folder in my asp .net web application.
Here two folders are in same application.I would like to copy these files dynamically at a particular time interval with out repetition.

Thank You... :)
Posted

As per my understanding, there are two ways.
1) Create a windows service and use Timer to do the coping/moving files.
2) Create a simple console application for file copy/move and use the windows task scheduler with intervals.

C#
var sourceFiles = Directory.GetFiles(SourceFolderPath , "*.txt", SearchOption.TopDirectoryOnly);
          foreach (var file in sourceFiles)
          {
              File.Move(file, DestinationFolderPath +"\\"+ Path.GetFileName(file));
          }


Thanks,
Raj
 
Share this answer
 
v3
Comments
Altaf Ansari89 12-Feb-15 4:56am    
string SourcePath = @"D:\Packages\"+ "Filename";
string DestPath= @"C:\Packages\"+ "Filename";

FileInfo f1 = new FileInfo(SourcePath);
if (f1.Exists)
{
File.Copy(SourcePath , DestPath);
}
try with this code :
C#
void HelperMethod()
        {
            System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1, 0, 0);
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {

            System.IO.DirectoryInfo dirinfo = new System.IO.DirectoryInfo("Source Folder Path");
            try {
                foreach (System.IO.FileInfo finfo in dirinfo.GetFiles())
                {
                    if (!System.IO.File.Exists("target Folder Path\\" + finfo.Name))
                    {
                        System.IO.File.Copy(finfo.FullName, "target Folder Path" + finfo.Name);
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
            finally { 
            }

        }
 
Share this answer
 
v3
I would suggest create FileSystemWatcher on specified folder...

how? check here http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

on new file created event you can started moving file from one location to another...

how? http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=VS.71).aspx

and i also suggest configure log4net for entry into text files...it gives flexibility of categorize message logging and slice on size...

check here
Here[^]
 
Share this answer
 
v3
use FileSystemWatcher in windows service there is an events of FileSystemWarcher that is FileSystemWatche_ Changed you can use your copy file code inside this event.
after using this approach you can also avoid time interval check for availability of file .
 
Share this answer
 
Quote:
tell me how to copy files from one folder to another folder in my asp .net web application


I think this is where you should rethink your solution. ASP.Net is a hosted runtime and as such it has abstraction layers between itself and the OS, which owns the filesystem.

Therefore you should not use a web application to control the file system.

There are several suggestions in this thread already of how to use Windows service technology and/ or filesystem watcher class instances to control the filesystem which you can use from csharp, but it should not execute from asp.net you should begin to forget that idea ;)

Otherwise the execution context of your application pool beeing in use by your website will need elevated rights which you shouldn't grant to something that can be executed from outside the box itself in such an accessible way like the HTTP(s) protocol.
 
Share this answer
 
Try with,

Code:
File.Copy("Source file path", "destination file path");


File derives from System.IO namespace
 
Share this answer
 
Use task scheduler that is available at your web hosting (if exists)
 
Share this answer
 
Create a windows service and use timer control to copy the files as required. Shown below is bare bones example.

C#
using System.Timers;

public Timer timer;

public MonitorService()
    {
        InitializeComponent();
        timer = new Timer(10000);//Time interval required by you.
        
    }

protected override void OnStart(string[] args)
    {
        // Hook up the Elapsed event for the timer.
        timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        timer.Enabled = true;
        timer.Start();
    }


private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
//Replace *.Ext  with required extention
var RequiredFiles = Directory.GetFiles(SourceFolderPath , "*.Ext");

          foreach (var file in RequiredFiles )
          {
              File.Move(file, DestinationFolderPath +"\\"+ Path.GetFileName(file));
          }
    }
 
Share this answer
 
v2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;

namespace movingfile
{
public partial class Class1
{
public Timer t1 = null;
public Class1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
t1 = new Timer();
this.t1.Enabled = true;
this.t1.Elapsed += new ElapsedEventHandler(t1_Tick);
this.t1.Interval = 10000;
}

protected override void OnStop()
{
this.t1.Enabled = false;
}

public void t1_Tick(object sender, EventArgs e)
{
File.Copy("c:\\SourceFile\\*.*", "D:\\DestFile\\*.*");


}
}
}
 
Share this answer
 
Dear SubhashRokzzz,

As I understand your question I suggest two ways


1. Write code in GLOBAL.ASAX --> if web application hosted on shared hosting.
2. Create console application --> if web application hosted on private/own server.


Beside that after reading your question I don't think so you are required code for coping folder or files.

Thanks & Regards,
Manoj Kalla
 
Share this answer
 
Comments
George Jonsson 5-Sep-15 4:18am    
You might have noticed that the question is a year old.
Manoj B. Kalla 7-Sep-15 1:36am    
Sorry, I forget to check.. I will take care next time. Thank you for reminding me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900