Replacing delimiter characters in file path

こ雲淡風輕ζ 提交于 2019-12-22 08:09:49

问题


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);

回答1:


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)))
{
     ...
}



回答2:


Are you sure the problem is the backslashes? Backslash is an escape character in strings, such that if you were adding it in a string you have to type it as "\\" rather than "\". (if you don't use @) Note that the debugger frequently displays the string the way you would put it in code, with the escape characters, rather than direct.

According to the documentation, Page.Request.PhysicalPath returns the path to the specific file you are in, not the directory. Directory.Exists is only true if you give it a directory, not a file. Does File.Exists() return true?




回答3:


You might want to consider replacing it with the Path.DirectorySeparatorChar rather than \ on the offchance that your code may end up running on a different platform one day (mono.net allows it to be run on linux or possibly more likely it might end up on some wierd mobile platform)

  • http://msdn.microsoft.com/en-us/library/system.io.path.directoryseparatorchar.aspx


来源:https://stackoverflow.com/questions/2641324/replacing-delimiter-characters-in-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!