在文件路径更换分隔符 [英] Replacing delimiter characters in file path

查看:316
本文介绍了在文件路径更换分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展在VS 2008 C#的Web应用程序我让用户选择一个输入文件,然后我存储在一个字符串变量的文件路径。但是,它存储此路径为C:\\folder\\ ......。所以我的问题是我怎么转换这个文件的路径为单个\?



感谢您为您的所有帮助家伙!请原谅我,因为我是一个新手,ASP.NET开发。这更多的是我在上下文中的代码。首先,我想看看该目录存在。我想我没有检查这一点,如果我检查文件是否存在。但是,这应该还是工作的权利?目前我的路径字符串变量没有显示出来我需要它的方式。我不知道如何制定本声明。最后,我要执行的ReadAllText声明(见最后一行)。

 保护无效btnAppend_Click(对象发件人,EventArgs五)
{
串FULLPATH = Page.Request.PhysicalPath;
串fullPath2 = fullpath.Replace(@\\,@\); (!Directory.Exists(fullpath2))

如果
{
弦乐味精=< H1>将上传路径不存在:{0}< / H1> ;;
的Response.Write(的String.Format(味精,fullpath2));
到Response.End();
}
路径字符串=@+ fullpath2 + uploadFile.PostedFile.FileName;

如果(File.Exists(路径))
{
//创建一个文件写入。

{
StreamReader的SR =新的StreamReader(路径);
字符串s =;
而(sr.Peek()&0)
S = sr.ReadLine();
sr.Close();
}
赶上(IOException异常EXC)
{
Console.WriteLine(exc.Message +无法打开文件);
的回报;
}
}

如果(uploadFile.PostedFile.ContentLength大于0)
{

= inputfile中有System.IO.File。 ReadAllText(路径);


解决方案

首先,调用 fullpath.Replace()无助于完整路径;它的返回的一个新的字符串。此外,当你的字符串在他们\(反斜线),你需要告诉编译器你的的尝试使用一个转义序列:

  =完整路径fullpath.Replace(@\\,@\); 



@ 的意思是请把这个字符串从字面上(逐字)。换句话说,当我说反斜杠,我的的意思是的反斜杠!



请参阅的 http://msdn.microsoft.com/en-us/library/362314fe.aspx



编辑:



由于LeBleu提到的,您呼叫Directory.Exists()在一个完整的文件路径。这将无法正常工作;你需要提取从路径的目录的一部分。试试这个:(!Directory.Exists(Path.GetDirectoryName(完整路径)))

 如果
{
...
}


I'm developing a C# web application in VS 2008. I let the user select an input file and then I store the file path in a string variable. However, it stores this path as "C:\\folder\\...". So my question is how do I convert this file path into single "\"?

Thank you guys for all your helps! Please forgive me as I am a newbie to ASP.NET development. This is more of my code in context. First I want to see if the directory exists. I guess I don't have to check this if I check if the file exists. But this should still work right? And currently my "path" string variable is not showing up the way I need it to. I'm not sure how to formulate this statement. Eventually I want to execute the ReadAllText statement (see the last line).

protected void btnAppend_Click(object sender, EventArgs e)
{
    string fullpath = Page.Request.PhysicalPath;
    string fullPath2 = fullpath.Replace(@"\\", @"\");

    if (!Directory.Exists(fullpath2))
    {
    string msg = "<h1>The upload path doesn't exist: {0}</h1>";
    Response.Write(String.Format(msg, fullpath2));
    Response.End();
}
    string path = "@" + fullpath2 + uploadFile.PostedFile.FileName; 

    if (File.Exists(path))
    {
        // Create a file to write to.
        try
        {
            StreamReader sr = new StreamReader(path);
            string s = "";
            while(sr.Peek() > 0)
                s = sr.ReadLine();
            sr.Close();
        }
        catch (IOException exc)
        {
            Console.WriteLine(exc.Message + "Cannot open file.");
            return; 
        }
    }

    if (uploadFile.PostedFile.ContentLength > 0)
    {

        inputfile = System.IO.File.ReadAllText(path);

解决方案

For a start, calling fullpath.Replace() does nothing to fullpath; it returns a new string. Also, when your string literals have a \ (backslash) in them, you need to tell the compiler that you're not trying to use an escape sequence:

fullpath = fullpath.Replace(@"\\", @"\"); 

The @ means "please treat this string literally (verbatim)". In other words, "when I say backslash, I mean backslash!"

See http://msdn.microsoft.com/en-us/library/362314fe.aspx.

Edit:

As LeBleu mentioned, you are calling Directory.Exists() on a full filepath. This won't work; you need to extract the directory part from the path. Try this:

if (!Directory.Exists(Path.GetDirectoryName(fullpath)))
{
     ...
}

这篇关于在文件路径更换分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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