| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1023 人关注过本帖
标题:为什么只可以显示同目录下的文件?
只看楼主 加入收藏
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
 问题点数:0 回复次数:11 
为什么只可以显示同目录下的文件?
如果我把生成的EXE放在桌面上运行,为什么我输入dir c:却只可以看见和EXE一起放在桌面上的文件?显示不了我要的目录?
全部显示其他的目录也可以但目录里的子目录里不能有这个EXE``
using System;

namespace Cmd
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
new Input();
}
}
}

搜索更多相关主题的帖子: 目录 文件 
2006-12-30 22:42
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
using System;
using System.IO;
namespace Cmd
{
/// <summary>
/// Input 的摘要说明。
/// 这个类处理输入
/// </summary>
public class Input
{
DirectoryInfo di=new DirectoryInfo(".");
public static string Currentpath;
bool Isrun=true;
public Input()
{
//
// TODO: 在此处添加构造函数逻辑
//
Currentpath=di.FullName;
Run();
}
void Run()
{
while(Isrun)
{
Console.Write(Currentpath+">");
string cmd=Console.ReadLine();
new Command(cmd);
}
}
}
}
2006-12-30 22:42
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
using System;
using System.IO;
namespace Cmd
{
/// <summary>
/// Command 的摘要说明。
/// 初步处理输入的数据,并交给check类进行是文件或者目录判断
/// </summary>
public class Command
{
string cmd;
string para;
public Command(string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
string []cmdarr=str.Trim().ToLower().Split(new char[]{' '});
cmd=cmdarr[0];
para=cmdarr[1];
object o=new Check(para).Do();
if(o==null)
{
Console.WriteLine("不存在的路径");
return;
}
else
new Dispose(cmd,o);//这里才开始进入命令处理

}
}
}
2006-12-30 22:43
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
using System;
using System.IO;
namespace Cmd
{
/// <summary>
/// Check 的摘要说明。
/// 检查参数是否存在,并装箱返回
/// </summary>
public class Check
{
string para;
public Check(string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.para=str;
}
public object Do()
{
try
{
DirectoryInfo di=new DirectoryInfo(para);
FileInfo fi=new FileInfo(para);
if(di.Exists)
return (object)di;
else if(fi.Exists)
return (object)fi;
else
{
return null;
}
}
catch(System.NotSupportedException)
{
return null;
}

}
}
}
2006-12-30 22:43
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
using System;
using System.IO;
using System.Collections;
namespace Cmd
{
/// <summary>
/// Dispose 的摘要说明。
/// </summary>
public class Dispose
{
string cmd;
//string para;
object o;
public Dispose(string cmd,object obj)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.cmd=cmd;
this.o=obj;
Run();
}
void Run()
{
switch (cmd)
{
case "cd":
cdcmd();
break;
case "dir":
dircmd();
break;
case "del":
delcmd();
break;
}
}
void cdcmd()
{
Input.Currentpath=((DirectoryInfo)o).FullName;
}
void dircmd()
{
ArrayList list=new ArrayList();
FileInfo[] fi=((DirectoryInfo)o).GetFiles();
foreach(FileInfo f in fi)
{
//Console.WriteLine(f.Name);
list.Add(f);
}
DirectoryInfo [] diarr=((DirectoryInfo)o).GetDirectories();
foreach(DirectoryInfo d in diarr)
{
//Console.WriteLine(d.Name+"\t\t<dir>");
list.Add(d);
}
new Output(list);
}
void delcmd()
{
try
{

((FileInfo)o).Delete();
Console.WriteLine("文件已经删除..");
}
catch(System.InvalidCastException)
{
Console.WriteLine("正在删除目录");
((DirectoryInfo)o).Delete(true);
Console.WriteLine("目录删除完毕");
}
}
}
}
2006-12-30 22:43
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
using System;
using System.Collections;
using System.IO;
namespace Cmd
{
/// <summary>
/// Output 的摘要说明。
/// 这个类处理输出
/// 最后的工作
/// </summary>
public class Output
{
ArrayList result;
public Output(ArrayList l)
{
//
// TODO: 在此处添加构造函数逻辑
//
result=l;
display();
}
void display()
{
foreach(Object o in result)
{
try
{
Console.WriteLine("\t\t"+((FileInfo)o).Name);
}
catch(System.InvalidCastException)
{
Console.WriteLine("<dir>\t\t"+((DirectoryInfo)o).Name);
}
}
}
}
}
2006-12-30 22:44
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
我知道问题在check类的do函数里,para是盘符("e:"),可是new 出来的directotinfo目录却变了 不知道是为什么?

2006-12-30 22:47
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
不过 也只有EXE存在的目录有这个问题,要看其他的目录却没问题,可以都显示出来
2006-12-30 22:50
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
得分:0 
最后的解决办法 把生成的EXE放在C盘跟目录下,不要放进任何文件夹里,好象只可以从上面找下来```不知道是为什么,是C#默认这样的?
2006-12-31 09:51
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 

是由于DirectoryInfo的构造函数的缺陷导致识别不出当前盘下的不完整的文件夹名.
只要做下面的更改就可以实现正常显示了.
public object Do()
{
try
{
DirectoryInfo di = new DirectoryInfo(para + "\\");//更改的地方
FileInfo fi = new FileInfo(para);
if (di.Exists)
return (object)di;
else if (fi.Exists)
return (object)fi;
else
{
return null;
}
}
catch (System.NotSupportedException)
{
return null;
}

}


实践、学习、再实践、再学习......
2006-12-31 21:27
快速回复:为什么只可以显示同目录下的文件?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012666 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved