Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am trying to copy a file from my c drive to a desktop but my code raised this error:
Quote:
System.IO.FileNotFoundException: Could not find file 'C:\TestFolder\test.txt'. File name


C#
public void fnEnvironment()
   {
       try
       {
           FileInfo someFile = new FileInfo(@"C:\TestFolder\test.txt");
           someFile.CopyTo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.txt");
           lblMsg.Text = "Has text.txt has been copied to the Desktop";
       }
       catch (Exception Ex)
       {
           lblError.Text = Ex.ToString();
           lblError.ForeColor = Color.Red;
       }

}
note that I have a folder called TestFolder and inside I have text.txt file.

Can someone help resolving this error.
Posted
Comments
Ron Beyer 15-Jan-14 18:07pm    
Please be specific about your operating system and what type of application (WPF, Metro, Win Forms, console). There are certain restrictions on some operating systems about what type of application can access from where (Metro can't access c:\ I believe).
El Dev 15-Jan-14 18:21pm    
It is a website(or webform)...

My best guess is you have hide file extensions for known file types on in windows. Your file probably is .txt.txt

also before your copy..

if (someFile.Exists)
{
//copy code here
}
 
Share this answer
 
v2
Comments
El Dev 15-Jan-14 18:22pm    
it has only .txt extension not .txt.txt
idenizeni 15-Jan-14 18:50pm    
Sidtrey may still be on to something, can you see the file extension for files in other directories? Are you sure your settings are set to show file extensions?
idenizeni 15-Jan-14 18:59pm    
You can also try using the below test code to list the files in the directory to verify this...

// code to list files
string[] files = Directory.GetFiles(@"C:\TestFolder\");
foreach (string file in files)
{
MessageBox.Show("Filename: " + file);
}
Hi Try like this..

1) Copy the URL and paste in the Windows Search ( Press Windows Key , paste in the text box ) and see whether the file appears over there .
if the file appears then the path is valid else check for the file extension and the directory..

2) put the file in some other location and try, other than C:
 
Share this answer
 
Thanks for your reply but I have resolve the issue,it was a permission issue...I have right click on the folder and check full control from the security Tab.Thanks anyway!!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 20:06pm    
Not an answer, abuse.
—SA

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