也许这是个无聊的问题,但我希望有人能在这里帮助我,因为我完全迷路了。我有一项工作要做(我是一名毕业程序员,他们给我设置了一种“挑战”)--别担心,我没有要求一个直接的答案,这也不是我日常工作的一部分,只是一种额外的工作。但我认为这涉及到界面,当涉及到他们时,我完全迷失了方向,所以我想知道是否有人能为我指明正确的方向。
基本上,我得到了一个Registry,它循环遍历注册表,并根据给定的参数在控制台应用程序上打印出键和值(参见下面的代码)
class RegistryList
{
public void RegistryWalker(RegistryKey _key, int _indent)
{
Output.RegistryOutPut(_indent, String.Format("Key: {0}", _key.Name.Split('\\').Last()));
string[] straValues = _key.GetValueNames();
foreach (string strValue in straValues)
{
RegistryValueKind kind = _key.GetValueKind(strValue);
Output.RegistryOutPut(_indent + 1, String.Format("Value: {0}", strValue));
}
string[] straSubKeys = _key.GetSubKeyNames();
foreach (string strSubKey in straSubKeys)
{
try
{
RegistryKey subKey = _key.OpenSubKey(strSubKey);
RegistryWalker(subKey, _indent + 2);
Thread.Sleep(200);
}
catch (System.Security.SecurityException)
{
Console.WriteLine("Denied Access");
}
}
}我不得不想出一个类似的文件,我已经完成了(再次见下文)
class FileList
{
public void FileWalker()
{
StringCollection log = new StringCollection();
string[] drives = Environment.GetLogicalDrives();
foreach (string dr in drives)
{
DriveInfo di = new DriveInfo(dr);
if (!di.IsReady)
{
Console.WriteLine("{0} could not be read", di.Name);
continue;
}
DirectoryInfo rootDir = di.RootDirectory;
Output.FileOutput(rootDir);
Console.WriteLine("Files with restricted access:");
foreach (string s in log)
{
Console.WriteLine(s);
}
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}为了记录在案,调用上面的代码片段中的Output.RegistryOutput和Output.FileOutput调用了单独的输出函数(很好,它们打印出每个注册表项和文件路径,但我需要两个函数--参见下面的注释)。
任务的最后一部分是将递归放入一个单独的函数中,该函数将接受一个FileWalker实例或一个RegistryWalker实例。有人在工作中给我指点界面来解决这个问题,但我完全迷失了方向。这两个函数将接受不同的参数,因此我无法看到单个接口将如何工作,因为如果要使用注册表遍历器,它需要一个RegistryKey和int值,但是文件遍历器没有参数。我已经创建了一个单一的IWalker接口(同样,在下面),但我不知道这两个函数如何工作。
interface IWalker
{
void Walker();
}有人能帮我弄清楚吗?即使这是正确的方法,我已经在这个问题上迷失了很长一段时间,如果可能的话,我肯定需要一些提示。
谢谢!
发布于 2013-11-30 17:49:00
最简单的方式,我认为这样的事情
interface IWalker
{
void Walker(object a=null, object b=null);
}
class RegistryList: IWalker
{
public void Walker(object a, object b){
var _key = (RegistryKey)a;
var _indent = Convert.ToInt32(b)
RegistryWalker(_key, _indent)
}
private void RegistryWalker(RegistryKey _key, int _indent)
{
....
}
}
class FileListIWalker
{
public void Walker(object a=null, object b=null){
FileWalker();
}
public void FileWalker(){...}
{发布于 2013-11-30 17:08:09
看看访问者的模式,希望能有所帮助。pattern
https://stackoverflow.com/questions/20302844
复制相似问题