C#中关于字符串处理的疑问

夏黑26 2009-12-01 02:52:54
string path = e.CommandArgument.ToString();
path = path.Replace(FileSystemManager.GetRootPath(), "~");
path = path.Replace("\\", "/");
Response.Redirect(path);

FileSystemManager.GetRootPath()这个方法实现的是活的根目录。代码如下:
private static string strRootFolder;

static FileSystemManager() {
strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath;
strRootFolder = strRootFolder.Substring(0,strRootFolder.LastIndexOf(@"\"));
}
/// <summary>
/// 读根目录
/// </summary>
/// <returns></returns>
public static string GetRootPath() {
return strRootFolder;
}

现在有疑问:第二行(加红色的那一行)的作用是什么呢,该行执行完之后path的值是什么呢?
根路径是:D:\Visual Studio 2005\projects\myProject\My\Web文件管理\Web文件管理
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jfan288 2009-12-01
  • 打赏
  • 举报
回复
就是把绝对路径转换成相对路径。
trunjun 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lyqin2008 的回复:]
引用 4 楼 trunjun 的回复:
static FileSystemManager() {
            strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath;
            strRootFolder = strRootFolder.Substring(0,strRootFolder.LastIndexOf(@"\"));
        }
第一个strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam\";
第二个
strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam";
去了后面个反斜杠。
path = path.Replace(FileSystemManager.GetRootPath(), "~");
意思是取到相对路径,项目的根目录。


能说说第二行,第三行分别执行后path的值吗?最好能写出来,谢谢!
[/Quote]
第二行执行后的值:path="~";
第三行执行后的值:path="~";
第四行执行后:指向Http://localhost:8888
夏黑26 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 trunjun 的回复:]
static FileSystemManager() {
            strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath;
            strRootFolder = strRootFolder.Substring(0,strRootFolder.LastIndexOf(@"\"));
        }
第一个strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam\";
第二个
strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam";
去了后面个反斜杠。
path = path.Replace(FileSystemManager.GetRootPath(), "~");
意思是取到相对路径,项目的根目录。
[/Quote]

能说说第二行,第三行分别执行后path的值吗?最好能写出来,谢谢!
jyqxj 2009-12-01
  • 打赏
  • 举报
回复
拿分走人
trunjun 2009-12-01
  • 打赏
  • 举报
回复
static FileSystemManager() {
strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath;
strRootFolder = strRootFolder.Substring(0,strRootFolder.LastIndexOf(@"\"));
}
第一个strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam\";
第二个
strRootFolder=@"E:\tangrj\exam\PhysicalApplicationPathExam\PhysicalApplicationPathExam";
去了后面个反斜杠。
path = path.Replace(FileSystemManager.GetRootPath(), "~");
意思是取到相对路径,项目的根目录。
夏黑26 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cjnkd 的回复:]
FileSystemManager.GetRootPath()  应该是一个目录,就是用“~”替换掉目录“FileSystemManager.GetRootPath()”;  Replace方法你可以在MSDN中查到,
[/Quote]
replace()方法我明白,我没有说清楚问题,我的理解第二行执行后path=“~”;第三行执行过后path没有发生变化,那他有什么用的,还是“~”在目录表示中有什么特殊的含义呢?第四行:Response.Redirect("~");值得跳转到哪啊!“~”在目录表示中指代什么啊
trunjun 2009-12-01
  • 打赏
  • 举报
回复
1分太少了,没人回答你,哈哈
不过我也给你测试下
cjnkd 2009-12-01
  • 打赏
  • 举报
回复
FileSystemManager.GetRootPath() 应该是一个目录,就是用“~”替换掉目录“FileSystemManager.GetRootPath()”; Replace方法你可以在MSDN中查到,

110,534

社区成员

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

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

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