文件路径的问题

gshuhugs 2008-07-16 09:20:57
当对文件进行操作时,有可能输入的路径不存在
如:“D://myFolder//folder//myDocument//project.txt”其中folder文件夹不存在,
出现这样的问题时,怎样在myFolder下创建folder文件夹。
谢谢。
...全文
99 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
stning 2008-07-16
  • 打赏
  • 举报
回复
string path = Path.GetDirectoryName("D://myFolder//folder//myDocument//project.txt");
if(Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
laozhao001 2008-07-16
  • 打赏
  • 举报
回复
//判断文件夹是否存在

if (!Directory.Exists(path))

{
//创建
Directory.CreateDirectory(path);

}

此方法可以!!!
ZGZice 2008-07-16
  • 打赏
  • 举报
回复
System.IO.Directory.CreateDirectory("D://myFolder//folder");
ZOU_SEAFARER 2008-07-16
  • 打赏
  • 举报
回复
呵呵,可以直接建立
不过是一个方法而已
lovefootball 2008-07-16
  • 打赏
  • 举报
回复
楼上的两位
CreateDirectory用的着递归么?
试过再说吧
ZOU_SEAFARER 2008-07-16
  • 打赏
  • 举报
回复
递归调用


private void CrearFolder(string path)
{
if (Directory.Exists(path) == true) //Folder is exist
{
return;
}
else
{
CreateFolder(path.Substring(0,path.LastIndexOf("\\")));//parent path
Directory.CreateDirectory(path);
}
}


string mypath = "D:\\myFolder\\folder\\myDocument";
private void button1_Click(object sender, EventArgs e)
{
CreateFolder(mypath);
}
ericzhangbo1982111 2008-07-16
  • 打赏
  • 举报
回复
string path="D://myFolder//folder//myDocument//project.txt";

string[] paths=Regex.Split(path,"//");

string p=paths[0];

for(int i=1;i<paths.Length;i++)
{
p=p+"//"+paths[i];
if(Directory.Exists(p))
{
p=p+"//"+paths[i];
}
else
{
Directory.CreateDirectory(p);
}

}
大概就这个意思
代码很多地方没考虑,自己完善下把
MyLf 2008-07-16
  • 打赏
  • 举报
回复
汗,很久没来了,抢问题都抢不过别人了.

一下子就跑到3楼来了.
宝_爸 2008-07-16
  • 打赏
  • 举报
回复
string rootDirectory = "..." //路径

if (!Directory.Exists(rootDirectory))
{
Directory.CreateDirectory(rootDirectory);
}
MyLf 2008-07-16
  • 打赏
  • 举报
回复
先判断是否存在,不存在则创建.

string path = Path.GetDirectoryName("D://myFolder//folder//myDocument//project.txt");
if(Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
初吻给了烟 2008-07-16
  • 打赏
  • 举报
回复
//判断文件夹是否存在

if (!Directory.Exists(path))

{
//创建
Directory.CreateDirectory(path);

}
贫僧又回来了 2008-07-16
  • 打赏
  • 举报
回复
System.IO.Directory.CreateDirectory(…………);

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧