Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class Program
{
static void Main(string[] args)
{
bool fileExists = false;

string thePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

string thefile = thePath + @"\y.txt;

fileExists = File.Exists(thefile);

if (fileExists)
{
Console.WriteLine("The file exists");

}
else
{
Console.WriteLine("The file does not exist,creating it");
File.Create(thefile);



}


if (fileExists)
{
string newpath = Environment.GetFolderPath (Environment.SpecialFolder.MyComputer);
string newfile = newpath + @"\newlife.text";
Console.WriteLine("It was created on {0}",File.GetCreationTime(thefile));
Console.WriteLine("It was last acessed on {0}",File.GetLastAccessTime(thefile));
Console.WriteLine("Moving the file...");
File.Move(thePath,newpath); // why i have error in this line???
}

Console.ReadLine();
}

}
Posted
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 18:45pm    
Error or exception? Please, complete exception or error information. File not found, or something?
--SA
mibetty 20-Nov-12 18:52pm    
the error is this :


A empty file name its not valid .
Name of the parameter: destFileName
Sergey Alexandrovich Kryukov 20-Nov-12 19:17pm    
This is not error, this is exception. Know the difference?
So, looks like newpath is empty. Is does not seem to be so in this code. Are you sure you show the same code which causes the problem? After all, execute it under the debugger, and you will see everything.
--SA

Your calculated file name is thefile, but you are using thePath in the call to File.Move. Always use the debugger. The code looks very fluffy, inaccurate and redundant. Why creating a file in one place and them moving. Generally, hard-coded file names are not useful, but you are using right things, such as Environment.SpecialFolder. You need to plan the code more thoroughly and make it mode combed…

—SA
 
Share this answer
 
I think Your problem is on this line


C#
string newpath = Environment.GetFolderPath (Environment.SpecialFolder.MyComputer);



The My Computer folder.

The MyComputer constant always yields the empty string ("") because no path is defined for the My Computer folder.

Please refer the following link..


http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^]
 
Share this answer
 
v2

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