简单的Directory问题
.Net 2.0, System.Direcotry.Exists("vv") == true????? --------------------编程问答-------------------- ?????楼主想干嘛? --------------------编程问答-------------------- System.IO.Directory.Exists("vv")== true --------------------编程问答--------------------
// For File.Exists, Directory.Exists--------------------编程问答-------------------- 哎 初学者什么都不会~ --------------------编程问答-------------------- 咳。。我的意思是:
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
System.Direcotry.Exists("vv")是检查当前目录下是否有folder vv,
有些情况希望检查的是完整的dir,怎么检查? --------------------编程问答-------------------- 本身你写的"vv"就应是一个完整的路径(绝对路径)
例如:Server.Mappath("~/ceshi");
返回的应该就是从盘符开始的路径 --------------------编程问答-------------------- vv是用户输入的,也许会输入错误的路径,所以需要检测
是否输入了盘符 --------------------编程问答-------------------- System.IO.Direcotry.Exists("VV")这样判断就可以啦呀。
当用户输入的路径不存在,那么返回的就是False了 --------------------编程问答--------------------
那就先检测“vv”是否是个正确的路径格式,
然后System.IO.Direcotry.Exists("vv"); --------------------编程问答-------------------- Exception objErr = Server.GetLastError().GetBaseException();
string absPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
absPath = absPath.Substring(System.Web.HttpContext.Current.Request.ApplicationPath.Length);
string _apppath = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.Replace(absPath, "").Split('?')[0];
得到完整路径 --------------------编程问答-------------------- 对呀 本身就是个完整的路径 不存在的就返回false的 --------------------编程问答-------------------- using System;
using System.IO;
public class TestClass
{
static void Main()
{
string dir=Console.ReadLine();
if(Directory.Exists(dir))
Console.WriteLine(dir);
else Console.WriteLine("Not Exist the Directory");
}
}
--------------------编程问答-------------------- 有点意思 --------------------编程问答-------------------- ...
算了这个结题了
对于用户输入的path需要检查是否有盘符
不然Directory.Exist("vv")会默认查找当前目录
Directory.Create("vv")也会创建到当前目录下
补充:.NET技术 , C#