File类中的错误错误行以粗体显示有人可以帮助我吗? [英] Error in File class the line of error is in BOLD Can someone help me?
问题描述
班级计划
{
静态void Main(string [] args)
{
bool fileExists = false;
字符串thePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
字符串thefile = thePath + @"\ y.txt;
fileExists = File.Exists(thefile);
如果(fileExists)
{
Console.WriteLine(文件存在");
}
其他
{
Console.WriteLine(文件不存在,正在创建");
File.Create(thefile);
}
如果(fileExists)
{
字符串newpath = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
字符串newfile = newpath + @"\ newlife.text";
Console.WriteLine(它是在{0}上创建的",File.GetCreationTime(thefile));
Console.WriteLine(上次在{0}上访问",File.GetLastAccessTime(thefile));
Console.WriteLine(正在移动文件...");
File.Move(thePath,newpath); //为什么我在此行中有错误??
}
Console.ReadLine();
}
}
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();
}
}
推荐答案
您计算出的文件名为thefile
,但是您在对File.Move
的调用中使用了thePath
.始终使用调试器.该代码看起来非常蓬松,不准确和多余.为什么要在一个地方创建文件并使它们移动.通常,硬编码的文件名没有用,但是您使用的是正确的东西,例如Environment.SpecialFolder
.您需要更彻底地计划代码,并使其模式组合起来.
—SA
Your calculated file name isthefile
, but you are usingthePath
in the call toFile.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 asEnvironment.SpecialFolder
. You need to plan the code more thoroughly and make it mode combed…
—SA
我认为您的问题在这条线上
I think Your problem is on this line
string newpath = Environment.GetFolderPath (Environment.SpecialFolder.MyComputer);
我的电脑文件夹.
MyComputer常量始终产生空字符串("),因为没有为我的电脑"文件夹定义任何路径.
请参考以下链接..
http://msdn.microsoft.com/zh-cn/library/system.environment.specialfolder.aspx [^]
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[^]
这篇关于File类中的错误错误行以粗体显示有人可以帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!