如何在asp.net中以特定间隔将文件从一个文件夹复制到另一个文件夹 [英] How do I copy files from one folder to another folder in particular intervals in asp .net

查看:103
本文介绍了如何在asp.net中以特定间隔将文件从一个文件夹复制到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的所有



请告诉我如何将文件从一个文件夹复制到我的asp .net网络应用程序中的另一个文件夹。

这里有两个文件夹在同一个应用程序中。我想以特定的时间间隔动态复制这些文件而不重复。



谢谢...... :) div class =h2_lin>解决方案

根据我的理解,有两种方法。

1)创建一个Windows服务并使用Timer来执行复制/移动文件。

2)为文件复制/移动创建一个简单的控制台应用程序,并间隔使用Windows任务调度程序。



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





谢谢,

Raj


尝试使用此代码:

  void  HelperMethod()
{
系统。 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( 源文件夹路径);
尝试 {
foreach (System.IO.FileInfo finfo dirinfo.GetFiles())
{
if (!System.IO.File.Exists ( target Folder Path \\ + finfo.Name))
{
System.IO.File.Copy(finfo.FullName, 目标文件夹路径 + finfo 。名称);
}
}
}
catch (例外情况)
{

}
最后 {
}

}


我建​​议在指定的文件夹上创建FileSystemWatcher ...



怎么样?点击此处 http://msdn.microsoft.com/en-us/library/system。 io.filesystemwatcher.aspx



新文件创建的事件你可以开始将文件从一个位置移动到另一个位置...



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



我还建议配置log4net进入文本文件...它提供了灵活的分类消息记录和切片大小...



查看此处

这里[ ^ ]


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... :)

解决方案

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.

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



Thanks,
Raj


try with this code :

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 { 
            }

        }


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[^]


这篇关于如何在asp.net中以特定间隔将文件从一个文件夹复制到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆