如何在C#中将新目录附加到现有网络路径? [英] How to append a new directory to an existing network path in C#?

查看:80
本文介绍了如何在C#中将新目录附加到现有网络路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将新目录附加到目标文件夹,这是一个网络路径



我的网络路径是\\\\
up-234 \\\ DD300 \OLD

为此我需要追加当前年份和月份,这意味着

\\\\
up-234 \\\\\\\\\\\\\\\\\\\\\\ 2016 \10然后将文件移到此处。



我尝试过:



I want to append a new directory to destination folder which is a network path

My network path is \\nup-234\TP\DD300\OLD
To this I need to append current year and month which means
\\nup-234\TP\DD300\OLD\2016\10 and then move the file to this.

What I have tried:

if (File.Exists(oldpathname + "\\" + filename))
                   {
                       string s = newpathname + "\\" + filename;
                       File.Move(oldpathname + "\\" + filename, newpathname + "\\" + filename);

                   }

推荐答案

在移动之前使用System.IO.Directory.CreateDirectory(newpathname)。 />


您不需要先检查它是否存在,如果不存在则创建路径中指定的所有目录,如果该目录已存在,则CreateDirectory不执行任何操作。 br $> b $ b

我希望我回答你的隐藏问题。
Use System.IO.Directory.CreateDirectory(newpathname) before the Move.

You don't need to check if it exists first, all directories specified in path are created if don't exists, if the directory already exists, CreateDirectory does nothing.

I hope I have answeres your "hidden" question.


这篇关于如何在C#中将新目录附加到现有网络路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆