Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
"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")
Posted
Updated 6-Nov-17 6:14am
v2
Comments
BillWoodruff 5-Nov-17 7:59am    
start here: https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied

your problem description is a bit confusing: you mention 'paste which is a user action in Windows by users, but you are using code here.
Member 12692000 5-Nov-17 10:09am    
the stackoverflow article did not solve the problem...is there some logic problems in the code? I'm having a hard time figuring out even though this program should have been easy and simple.
Member 12692000 5-Nov-17 8:08am    
I meant copy the files to the desired folder...my bad

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!
 
Share this answer
 
Comments
Member 12692000 5-Nov-17 7:44am    
If I could figure the problem out myself and solve it then why would I post it here?
Its not that I'm asking people to write the entire code, just point out or tweak my code to make it work if they are willing to.

Anyways thanks for your opinion...
OriginalGriff 5-Nov-17 7:55am    
The problem is that we can't do it for you: the error message makes that very clear.
Some part of one of your paths is not accessible because the process executing that code does not have sufficient permission to either read or write a particular folder.

I don't have that folder; I don't have the user ID that the process runs under. So I can't run your code using your files list and identify what the folder that is causing you problems is. And when I haven't done that, I can't manually examine the permissions in the folders in the path I can't read to see where you need to alter the user or the permissions.

Only you can do that: only you have access to the machine you are running that code on...
So drop the attitude, please.
Member 12692000 5-Nov-17 8:14am    
I'm not showing any attitude...relax dude.
I get your point.
OriginalGriff 5-Nov-17 8:24am    
:D
See if this will fix the file access problem:

1. add this to your app's 'using statements:
C#
using System.Security.Permissions;
2. add this Attribute just above the method(s) where you are getting access denied errors now:
C#
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void MyFileAccessMethod(?,?,?)
{
}
 
Share this answer
 
Comments
Member 12692000 5-Nov-17 10:05am    
It does not solve the problem...the errors are still there...
BillWoodruff 5-Nov-17 15:02pm    
Also, try running Visual Studio as 'Administrator. Is Drive 'D on a network ?
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:
C#
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);
        }
    }
}
 
Share this answer
 
Comments
Member 12692000 7-Nov-17 0:10am    
Thanks Richard :D

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