新手挣扎着一个简单的程序? [英] Newbie struggling with a simple program?

查看:68
本文介绍了新手挣扎着一个简单的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我正在尝试创建一个简单的文件复制程序,将* .tif文件从一个位置 from_path 复制到另一个 to_path

如果 from_path 类似于D:\database,那么其中的内容看起来像是

D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\等等



等...

如果 to_path 就像E:\ JOobs ,然后里面的内容看起来像

E:\Jobs \12334-12366 \\\ xml \ 12345.xml
E:\ Jobs \ 13334-12666 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ big>来自 from_path 中文件夹的文件,其名称与 to_path 中的xml文件名完全相同

以下是方法我试图实现

1)从 to_path 获取所有* .xml文件

2)获取xml文件的名称和完整路径

3)检查 from_path 中与xml文件名称相同的文件夹是否存在

4)如果是这样的话,从中复制所有 tif 文件并使用步骤2中的完整路径粘贴到名为xml的文件夹中。

我尝试过一些东西如下所示,但我得到 System.UnauthorizedAccessException:访问路径D:\database \ 12335'被拒绝

另外 targetLoc 在我的程序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(已完成)

解决方案

从调试器开始:放置一个断点方法的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


抱歉,但我们不能为您做到这一点,因为我们没有您的文件或文件夹结构 - 您有时间学习一种新的(非常非常有用的)技能:调试!


< blockquote>看看这是否能解决文件访问问题:



1.将此添加到你的应用程序的'using statements:

 使用 System.Security.Permissions; 

2。将此属性添加到您现在获得访问被拒绝错误的方法的上方:

 [PermissionSet(SecurityAction.Demand,Name =   FullTrust)] 
private void MyFileAccessMethod(?,?,?)
{
}


这里有几点混乱:



1) Path.GetFullPath(xmlFile.Name)将XML文件的名称与当前工作目录相结合。这将与XML文件的完整路径相同,您可以使用 xmlFile.FullName ;
$ b检索XML文件的完整路径$ b

2)你实际上并不想要XML文件的完整路径;因为您正在尝试将其他文件复制到路径,所以您需要父目录的路径。你可以使用 xmlFile.DirectoryName 检索它;



3)因为 tifLoc 是一个目录,你不能使用 File.Copy 将其复制到别处。不幸的是,您需要单独复制单个文件。



4) Directory.GetDirectories(...)。FirstOrDefault()将返回 null 。在这种情况下,当您尝试调用 .ToString()时,您将在下一行获得 NullReferenceException 在返回的字符串值。



另外,你可能想要使用 EnumerateFiles EnumerateDirectories 而不是他们的获取* 对应物,特别是如果有很多要处理的文件。 Get * 版本必须先将所有匹配项加载到内存中才能开始处理它们,而 Enumerate * 版本让你找到第一个匹配就开始处理了。



这样的东西应该有效:

 DirectoryInfo diCopyFrom =  new  DirectoryInfo(textBox1.Text); 
DirectoryInfo diCopyTo = new DirectoryInfo(textBox2.Text);

foreach (FileInfo xmlFile in diCopyTo.EnumerateFiles( * .xml,SearchOption.AllDirectories))
{
// 例如:
// xmlFile =E:\ Jobs \ 12334-12366 \\\ xml \ 12345.xml

string tifDirectoryName = Path.GetFileNameWithoutExtension(xmlFile.Name);
// tifDirectoryName =12345

DirectoryInfo sourceDirectory = diCopyFrom.EnumerateDirectories(tifDirectoryName,SearchOption.AllDirectories).FirstOrDefault();
if (sourceDirectory!= null
{
string destinationDirectoryName = xmlFile.DirectoryName;
// destinationDirectoryName =E:\Jobs \12334-12366 \\\\ xml \\ \\

foreach (FileInfo tifFile in sourceDirectory.EnumerateFiles ( * .tif))
{
// tifFile =D:\database\12345\tiffile1.tif

string destFileName = Path.Combine(destinationDirectoryName,tifFile.Name);
// destFileName =E:\Jobs \12334-12366 \\\\ xml \\ \\ tiffile1.tif

tifFile.CopyTo(destFileName, true );
}
}
}


Hi, all
I'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 tif files from a folder in from_path whose name is exactly the same as the name of the xml file in to_path
Below is the method that I tried to implement
1) get all *.xml files from to_path
2) get the name and full path of the xml file
3) check whether the folder with the identical name as the xml file in from_path exist
4) if so copy all tif 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 targetLoc 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")

解决方案

Start with the debugger: put a breakpoint on the first line in the method, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you because we don't have your files or folder structures - time for you to learn a new (and very, very useful) skill: debugging!


See if this will fix the file access problem:

1. add this to your app's 'using statements:

using System.Security.Permissions;

2. add this Attribute just above the method(s) where you are getting access denied errors now:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void MyFileAccessMethod(?,?,?)
{
}


Several points of confusion here:

1) Path.GetFullPath(xmlFile.Name) will combine the name of the XML file with the current working directory. That will not be the same as the full path of the XML file, which you can retrieve using xmlFile.FullName;

2) You don't actually want the full path of the XML file; since you're trying to copy other files to the path, you want the path of the parent directory. You can retrieve that using xmlFile.DirectoryName;

3) Since tifLoc is a directory, you can't use File.Copy to copy it elsewhere. Unfortunately, you need to copy the individual files separately.

4) Directory.GetDirectories(...).FirstOrDefault() will return null if the directory doesn't exist. In that case, you'll get a NullReferenceException on the next line, when you try to call .ToString() on the returned string value.

Also, you'll probably want to use EnumerateFiles and EnumerateDirectories instead of their Get* counterparts, particularly if there are a lot of files to process. The Get* versions have to load all matches into memory before you can start processing them, whereas the Enumerate* versions let you start processing as soon as the first match is found.

Something like this should work:

DirectoryInfo diCopyFrom = new DirectoryInfo(textBox1.Text);
DirectoryInfo diCopyTo = new DirectoryInfo(textBox2.Text);

foreach (FileInfo xmlFile in diCopyTo.EnumerateFiles("*.xml", SearchOption.AllDirectories))
{
    // Eg:
    // xmlFile = "E:\Jobs\12334-12366\1\xml\12345.xml"
    
    string tifDirectoryName = Path.GetFileNameWithoutExtension(xmlFile.Name);
    // tifDirectoryName = "12345"
    
    DirectoryInfo sourceDirectory = diCopyFrom.EnumerateDirectories(tifDirectoryName, SearchOption.AllDirectories).FirstOrDefault();
    if (sourceDirectory != null)
    {
        string destinationDirectoryName = xmlFile.DirectoryName;
        // destinationDirectoryName = "E:\Jobs\12334-12366\1\xml\"
        
        foreach (FileInfo tifFile in sourceDirectory.EnumerateFiles("*.tif"))
        {
            // tifFile = "D:\database\12345\tiffile1.tif"
            
            string destFileName = Path.Combine(destinationDirectoryName, tifFile.Name);
            // destFileName = "E:\Jobs\12334-12366\1\xml\tiffile1.tif"
            
            tifFile.CopyTo(destFileName, true);
        }
    }
}


这篇关于新手挣扎着一个简单的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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