views:

191

answers:

3

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);
+4  A: 

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)))
{
     ...
}
Charles
Thanks Charles, but when I try this in the Watch window it is still showing the double "\". In other words it's not replacing anything.
salvationishere
Put the code you're using now in your question. The code in my example will definitely convert double-backslash to single-backslash.
Charles
Charles, I'm not sure I understand your explanation here. However, I updated my question so maybe you will see what I am eventually trying to achieve.
salvationishere
Thanks for your help, Charles. I solved my problem! (see below).
salvationishere
I'm not sure how you fixed it, because the latest code you posted is even worse! I guess you realized that *you dont need to append "@" to a non-literal string*. Only use that @ trick on string literals.
Charles
A: 

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)

rtpHarry
but then he would have no reason to worry about slashes, no?
drachenstern
Thank you guys for all your helps! I solved my problem. All I did was removed the "@" character so that my file path would treat "\\" as "\"! Simple, heh?
salvationishere
Ah, good. See my last comment on my answer. Also note my last edit!
Charles
+5  A: 

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?

LeBleu
Thanks for your helps; could you look at my revised question?
salvationishere
problem solved. Thanks!
salvationishere