如何在C#中使用Path.Combine删​​除文件 [英] How to work with Path.Combine in C# to delete a file

查看:65
本文介绍了如何在C#中使用Path.Combine删​​除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string root = Server.MapPath("~");
                string path = "\\Brands\\" + Session["brandname"].ToString() + "\\" + Session["departmentname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + Session["groupname"].ToString() + "\\" + item;
                string originalpath = System.IO.Path.Combine(root, path);
                bool isExists = System.IO.Directory.Exists(Server.MapPath(@"~\\" + originalpath));
                if (!isExists)
                {
                    chkstyles.Items.RemoveAt(i);
                    File.Delete(@originalpath); 
                }







预期输出:

root = E :\ sg

path = \ Brands\a\b\c

originalpath = E\sg\\\\\\\ \\ b



但是产生了C\\\\\\ ....没有得到当前的访问文件夹路径。但是有了这个,我在所有代码中工作,这些代码只在一个模块上工作得很好而且很忙。不知道它是如何工作的,而不是在这里工作。




Expected Output:
root=E:\sg
path=\Brands\a\b\c
originalpath=E\sg\a\b\c\d

but produced C\a\b\c .... Didn't get the current accessing folder path. But with this, I worked in all codes which worked fine but hectic at one module alone. Doesn't know how it worked there and not working here.

推荐答案

唔不...它不会起作用。

看看你正在做什么:

Well no...it's not going to work.
Look at what you are doing:
string root = Server.MapPath("~");

哪个给你E:\ sg

Which gives you "E:\sg"

string originalpath = System.IO.Path.Combine(root, path);

哪个给你 E:\ sg \\\\\\\

然后,你这样做:

Which gives you "E:\sg\a\b\c\d"
Then, you do this:

bool isExists = System.IO.Directory.Exists(Server.MapPath(@"~\\" + originalpath));

检查是否存在名为E:\ sgE:sg \\\ \\c



哪个会失败...



你为什么要尝试包括根文件夹两次?

Which is checking to see if a file exists called "E:\sgE:sg\a\b\d\c"

Which is going to fail...

Why are you trying to include the root folder twice?


这篇关于如何在C#中使用Path.Combine删​​除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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