ASP.NET如何实现把某一目录下文件挪到另一个目录?

ASP.NET如何实现把某一目录下文件挪到另一个目录?,第1张

strin path="原文件夹路径

string dpath="目标文件夹路径"

if(Directory.Exist(path))

{   

string[] files = Directory.GetFiles(path, "*.doc",

System.IO.SearchOption.AllDirectories)

 foreach(string str in files)

{

FileInfo info=new FileInfo(str) 

//省略目录及文件是否存在的判断    

info.MoveTo(dpath) 

  }  

  

}

但需要注意目录的权限。

给你个例子:

public Form1()

{

InitializeComponent()

this.AllowDrop = true

this.DragEnter += new DragEventHandler(Form1_DragEnter)

this.DragDrop += new DragEventHandler(Form1_DragDrop)

}

void Form1_DragEnter(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop))

{

e.Effect = DragDropEffects.All

}

else

{

e.Effect = DragDropEffects.None

}

}

void Form1_DragDrop(object sender, DragEventArgs e)

{

// 这里写你要处理的代码。

}

给你个例子,代码比较乱,凑合看看吧

string path = @"c:\temp\MyTest.txt"

string path2 = @"c:\temp2\MyTest.txt"

try

{

if (!File.Exists(path))

{

throw new Exception("file not exist")

}

// Ensure that the target does not exist.

if (File.Exists(path2))

File.Delete(path2)

// Move the file.

File.Move(path, path2)

Console.WriteLine("{0} was moved to {1}.", path, path2)

// See if the original exists now.

if (File.Exists(path))

{

Console.WriteLine("The original file still exists, which is unexpected.")

}

else

{

Console.WriteLine("The original file no longer exists, which is expected.")

}

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString())

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8009227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存