2013-06-24 21 views
1

我正在尝试在C#中编写一个项目Windows服务。将DirectoryInfo转换为Windows服务中的字符串

我想将文件夹复制到另一个目录。我写的代码,一切都是完美的

DirectoryInfo source = new DirectoryInfo("C:\\belgeler"); 
DirectoryInfo target = new DirectoryInfo("E:\\Backup"); 

这是正确执行,但是当我写这篇文章...

DirectoryInfo source = new DirectoryInfo(from_path); 
DirectoryInfo target = new DirectoryInfo(to_path); 

的错误是“A字段初始不能引用非静态 字段,方法或属性 'BACKUP(m​​yproject_name).Service1.veri'

//string to_path = Registry.LocalMachine.GetValue("ToPath").ToString(); 
//string from_path = Registry.LocalMachine.GetValue("FromPath").ToString(); 

此代码块正在工作控制台应用程序,但在Windows服务中不是。

+1

它是一个非静态的字段,方法或属性吗? ;) –

+0

“此代码块正在工作控制台应用程序,但在Windows服务中不是。”它是COMPILING作为一个控制台应用程序,但不是一个Windows服务?我非常怀疑...... – aquinas

+0

是你的类的'from_path'和'to_path'成员,还是它们是局部变量?那么'source'或'target'呢? –

回答

2

您的变量sourcetarget是您班级的成员变量。以下代码是允许的:

DirectoryInfo source = new DirectoryInfo("C:\\belgeler"); 
DirectoryInfo target = new DirectoryInfo("E:\\Backup"); 

它被允许是因为它没有引用您的类的任何其他成员变量。但是,当你尝试:

DirectoryInfo source = new DirectoryInfo(from_path); 
DirectoryInfo target = new DirectoryInfo(to_path); 

此引用其他变量from_pathto_path这是不允许的。

将这些变量移到局部变量,这应该可以解决您的问题。