本文介绍了新手挣扎着一个简单的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所有 我正在尝试创建一个简单的文件复制程序,将* .tif文件从一个位置 from_path 复制到另一个 to_path 。 如果 from_path 类似于D:\database,那么其中的内容看起来像是 D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\等等 等... 如果 to_path 就像E:\ JOobs ,然后里面的内容看起来像 E:\Jobs \12334-12366 \\\ xml \ 12345.xmlE:\ Jobs \ 13334-12666 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ big>来自 from_path 中文件夹的文件,其名称与 to_path 中的xml文件名完全相同 以下是方法我试图实现 1)从 to_path 获取所有* .xml文件 2)获取xml文件的名称和完整路径 3)检查 from_path 中与xml文件名称相同的文件夹是否存在 4)如果是这样的话,从中复制所有文件并使用步骤2中的完整路径粘贴到名为xml的文件夹中。 我尝试过一些东西如下所示,但我得到 System.UnauthorizedAccessException:访问路径D:\database \ 12335'被拒绝。 另外在我的程序bin目录中显示文件的路径。 我尝试了什么: string to_path = textBox1.Text; string from_path = textBox2.Text; DirectoryInfo diCopyFrom = new DirectoryInfo(from_path); DirectoryInfo diCopyTo = new DirectoryInfo(to_path); FileInfo [] xmlFiles = diCopyTo.GetFiles(*。xml,SearchOption.AllDirectories); foreach(xmlFiles中的FileInfo xmlFile) { var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name); var targetLoc = Path.GetFullPath(xmlFile.Name); var tifLoc = Directory.GetDirectories(from_path,xmlDir,SearchOption.AllDirectories).FirstOrDefault(); if(Directory.Exists(tifLoc.ToString())) File.Copy(tifLoc,targetLoc,true); } MessageBox.Show(已完成) 解决方案 看看这是否能解决文件访问问题: 1.将此添加到你的应用程序的'using statements: 使用 System.Security.Permissions; 2。将此属性添加到您现在获得访问被拒绝错误的方法的上方: [PermissionSet(SecurityAction.Demand,Name = FullTrust)] private void MyFileAccessMethod(?,?,?) {} Hi, allI'm trying to create a simple file copy program to copy *.tif files from one location from_path to another to_path.Say, if from_path is something like "D:\database", then contents inside it looks like"D:\database\12345\tiffile1.tif, tiffile2.tif ... etc""D:\database\12335\tiffile1.tif, tiffile2.tif ... etc"so on...and if to_path is something like "E:\Jobs", then contents inside it looks like"E:\Jobs\12334-12366\1\xml\12345.xml""E:\Jobs\13334-12666\15\xml\12335.xml"I want to copy all files from a folder in from_path whose name is exactly the same as the name of the xml file in to_pathBelow is the method that I tried to implement1) get all *.xml files from to_path2) get the name and full path of the xml file3) check whether the folder with the identical name as the xml file in from_path exist4) if so copy all files from it and paste inside the folder named xml using the full path in step 2.I've tried something like below but I'm getting System.UnauthorizedAccessException: Access to the path D:\database\12335' is denied.Also the is showing path of the file in my programs bin directory.What I have tried:string to_path=textBox1.Text;string from_path=textBox2.Text;DirectoryInfo diCopyFrom = new DirectoryInfo(from_path);DirectoryInfo diCopyTo = new DirectoryInfo(to_path);FileInfo[] xmlFiles = diCopyTo.GetFiles("*.xml",SearchOption.AllDirectories);foreach (FileInfo xmlFile in xmlFiles){var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name);var targetLoc=Path.GetFullPath(xmlFile.Name);var tifLoc = Directory.GetDirectories(from_path,xmlDir,SearchOption.AllDirectories).FirstOrDefault();if (Directory.Exists(tifLoc.ToString()))File.Copy(tifLoc, targetLoc, true);}MessageBox.Show("Finished") 解决方案 这篇关于新手挣扎着一个简单的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 15:30