using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace IOZuoye
{
class Program
{
public bool isPath(string path)
{
DriveInfo[] dr = DriveInfo.GetDrives(); bool isPath_ = false;
foreach (DriveInfo d in dr)
{
if (path.ToUpper().IndexOf(d.RootDirectory.FullName.ToUpper()) == 0)
{
isPath_ = true;
return isPath_;
}
}
return isPath_;
}
public static void Main()
{
Program p = new Program();
bool b = p.isPath("e:\\asdf/asf");
Console .WriteLine (b);
}
}
}
还是刚才我字符串的问题,特此我把问题重新贴一下
假设有一个字符串 path ,如何判断它是不是一个合法的路径?
PS:path 可以是不存在的路径,只要合法就可以。
例如:path="e:\\abc\c\a.txt";
可能E盘下没有这个文件夹,但是它可以被创建,所以就合法。
再如: path="f:\\a.txt";如果我的电脑根本就没有F盘,这个文件不可能被创建,也就不合法。
谢谢了。
满意答案马上给分!
解决方案 »
- Datagridview单元绑定问题?
- 如何获取添加到委托中的类型
- 2005.Net serialPort控件问题
- 基于Form的程序的按钮,如何把按钮四周的线框去掉?
- 欢迎大家去我的个人主页http://www.netfocus.cn看看是否有你想要的
- webBrowser
- 关于FreeTextBox中的大文本内容插入Sql数据库出错的问题
- Graphics.DrawString的一些问题
- 难题求解!!! 如何让一个非模态的 Form 只对父窗体 TopMost 而不对其他应用程序的窗口 TopMost?
- 200分等着你拿,讨论关于用C#开发网上发短信的问题
- 一个比较长的字符串,是固定不变的,不会做其它操作,用string效率好,还是用stringbuilder的append连接起来效率好
- 一道C#笔试题,大家帮我看看,电脑上没有环境,不知道答案,知道的大哥麻烦写一下原理
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace IOZuoye
{
class Program
{
public bool isPath1(string path)
{
bool isp = Directory.Exists(path);
return isp ;
}
public static void Main()
{
Program p = new Program();
bool c = p.isPath("h:\\abc//");
Console.WriteLine(c);
}
}
}
返回值是TRUE
try一下 成功了 就再把它删除 不成功就是不合法了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace IOZuoye
{
class Program
{
public bool isPath(string path)
{
private bool ExistsPath(string path)
{
try
{
Directory.CreateDirectory(path);
Directory.Delete(path);
return true;
}
catch
{
return false;
}
}
public static void Main()
{
Program p = new Program();
bool d = p.ExistsPath("h:\\abc//");
}
}
}
按照4楼的说法,返回值为NULL
函数名:判断路径是否合法
输入参数:路径,string
返回值:是否合法,true \false内部使用路径和规则比较
private bool ExistsPath(string path)
{
try
{
Directory.CreateDirectory(path);
Directory.Delete(path);
return true;
}
catch
{
return false;
}
}
外面就不要再套东西了
{
if (System.IO.Directory.Exists(path))
return true;
try
{
System.IO.Directory.CreateDirectory(path);
System.IO.Directory.Delete(path);
return true;
}
catch
{
return false;
}
}前面加个判断保险点,否则会删错