返回信息流using System;
using System.IO;
class DirectoryTest
{
public static void Main()
{
Directory d = new Directory("D:\\C#");
Directory d1;
Directory d2;
try
{
d1 = d.CreateSubdirectory("file1");
}
catch (IOException e)
{
Console.WriteLine("directory file1 failed because:{0}",e);
return;
}
try
{
d2 = d.CteateSubdirectories("file1\\file2");
}
catch (IOException e)
{
Console.WriteLine("directory file2 failed because:{0}",e);
return;
}
Console.WriteLine("Create directory successfully!");
}
}
这是书上的一个例子,但是报什么静态方法的错误...麻烦指导下Directory类咋用啊...这个例子有没有问题啊...
这是一条镜像帖。来源:北邮人论坛 / dot-net / #2247同步于 2010/8/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
dotNET机器人发帖
Directory用法
buptxiaofeng
2010/8/15镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
这是。。 VBA么?
我猜是你在静态方法中用了非静态的变量
【 在 buptxiaofeng (萧风) 的大作中提到: 】
: using System;
: using System.IO;
: class DirectoryTest
: ...................
【 在 qixi 的大作中提到: 】
: 这是。。 VBA么?
: 我猜是你在静态方法中用了非静态的变量
: 【 在 buptxiaofeng (萧风) 的大作中提到: 】
: ...................
C#...照着书抄的...那个错误说的大概就是你说的那个意思...
C#用个dictionary不用这么麻烦吧……直接new一下筐吃筐吃往里add就行了……
【 在 buptxiaofeng (萧风) 的大作中提到: 】
: C#...照着书抄的...那个错误说的大概就是你说的那个意思...
【 在 qixi 的大作中提到: 】
: C#用个dictionary不用这么麻烦吧……直接new一下筐吃筐吃往里add就行了……
: 【 在 buptxiaofeng (萧风) 的大作中提到: 】
: : C#...照着书抄的...那个错误说的大概就是你说的那个意思...
: ...................
能麻烦举个例子么...谢谢谢谢...刚学...见笑了...
发现我看错单词了,sorry
在百度上搜的一个解答,你看看合不合用
无法声明静态类型“System.IO.Directory”的变量
以前看到一个教程,估计是老版的.net下写的,有用到Directory 实例化变量,类似 Directory strDir = null;这在新版的.net中是不被允许的。因为Directory是静态类型的变量了。正如错误信息所提示的:“无法声明静态类型“System.IO.Directory”的变量”,“无法创建静态类“System.IO.Directory”的实例”,一切皆因为System.IO.Directory是静态的类型,不懂什么是静态类型的,去补一下基础。
你需要做的就是类似下面这样的操作string[] strDrivers = Directory.GetLogicalDrives();
直接用类名加上属性或方法就可以达到要求了。
string[] strDrivers = Directory.GetLogicalDrives();
就是取得驱动盘符列表。
【 在 buptxiaofeng (萧风) 的大作中提到: 】
: 能麻烦举个例子么...谢谢谢谢...刚学...见笑了...
赞!
lz可以查下 Directory定义,一个类被标识为static,就不能实例化一个Directory实例了。
[ComVisible(true)]
public static class Directory
{
//....
}
贴两段MSDN上的代码,lz可以研究下
-------------------
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx
[C#]
using System;
using System.IO;
class Test
{
public static void Main()
{
// Specify the directory you want to manipulate.
string path = @"c:\MyDir";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
Console.WriteLine("That path exists already.");
return;
}
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}
-------------------
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.createsubdirectory(v=VS.71).aspx
[C#]
using System;
using System.IO;
public class CreateSubTest
{
public static void Main()
{
// Create a reference to a directory.
DirectoryInfo di = new DirectoryInfo("TempDir");
// Create the directory only if it does not already exist.
if (di.Exists == false)
di.Create();
// Create a subdirectory in the directory just created.
DirectoryInfo dis = di.CreateSubdirectory("SubDir");
// Process that directory as required.
// ...
// Delete the subdirectory.
dis.Delete(true);
// Delete the directory.
di.Delete(true);
}
}
-------------------
【 在 qixi 的大作中提到: 】
: 发现我看错单词了,sorry
: 在百度上搜的一个解答,你看看合不合用
: 无法声明静态类型“System.IO.Directory”的变量
: ...................
如果非要new的话
可以用directoryinfo这个东东
当然,这两个东西还是有不同的,
至于具体有什么不同,就要仔细研究研究了。
【 在 qixi (七夕‖桂香南疆好地方) 的大作中提到: 】
: 发现我看错单词了,sorry
: 在百度上搜的一个解答,你看看合不合用
: 无法声明静态类型“System.IO.Directory”的变量
: ...................