1

for some reason when I create my path that will be used for my StreamWriter it creates a folder called test.doc instead of a file called test.doc

Here is my code:

fileLocation = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "QuickNote\\");
fileLocation = fileLocation + "test.doc";

Can anyone tell me what I'm doing wrong with my file path?

UPDATE:

class WordDocExport
{
    string fileLocation;
    public void exportDoc(StringBuilder sb)
    {
        fileLocation = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "QuickNote\\");
        fileLocation = fileLocation + "test.doc";

        if (!Directory.Exists(fileLocation))
        {
            Directory.CreateDirectory(fileLocation);
            using (StreamWriter sw = new StreamWriter(fileLocation, true))
            {
                sw.Write(sb.ToString());
            }
        }

        else
        {
            using (StreamWriter sw = new StreamWriter(fileLocation, true))
            {
                sw.Write(sb.ToString());
            }
        }
    }
}

Sorry for the delay. I posted the question this morning right before I left for work and was in such as hurry that I didn't even think to post the rest of my code. So, here it is. Also I attempted to do a Path.Combine on the 2nd line test.doc but it gives the same problem.

Pallas
  • 1,499
  • 5
  • 25
  • 57
  • 3
    What does the streamwriter look like? Also please post the entire fileLocation string as it looks at runtime. – asawyer Aug 21 '12 at 12:10
  • 1
    Never heard that StreamWriter creates directory. Could you show more of your code? – Steve Aug 21 '12 at 12:15
  • 1
    This is not the relevant code for the problem. Also the title hints at yet another issue. – H H Aug 21 '12 at 12:17
  • Did you do some basic debugging of what the values are of your variables? If so, please provide this information as well (but as I can read below, the answers, the mystery probably is already solved). Apart from that, your title really doesn't match your post... I don't see anywhere any information related to your title in your post. Suggestion, read this blog post: http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Styxxy Aug 21 '12 at 12:29
  • 1
    It is not true that Path.Combine remove the ending slash- So this is not the problem. I think that OP, erroneously, creates a directory using the constructed filename but we haven't see any clarification from him. – Steve Aug 21 '12 at 12:43

4 Answers4

4

OK, after seeing the complete code:

    fileLocation = fileLocation + "test.doc";

    if (!Directory.Exists(fileLocation))
    {
        Directory.CreateDirectory(fileLocation);     // this is the _complete_ path
        using (StreamWriter sw = new StreamWriter(fileLocation, true))
        {
            sw.Write(sb.ToString());
        }
    }

You are literally calling CreateDirectory with a string ending in "test.doc". It does not matter if a path ends with \ or not, and "<something>\QuickNote\test.doc" is a valid folder path.

You can replace the code with:

string rootFolderPath = Environment.GetFolderPath(
    System.Environment.SpecialFolder.MyDocuments);

string folderPath = Path.Combine(rootFolderPath, "QuickNote");

if (!Directory.Exists(folderPath))
{
    Directory.CreateDirectory(folderPath);
}

fileLocation = Path.Combine(folderPath, "test.doc");

using (StreamWriter sw = new StreamWriter(fileLocation, true))
{
    sw.Write(sb.ToString());
}

     

No need to create a writer twice.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Good answer Hank, but in the future you might want to work on your tact and how you address others on this forum. It's people like you that make people want to stop contributing. I realize you probably figure you sit on high because you have 108K but be careful how you think of yourself and others lest we forget Jon Skeet. – Mike Perrenoud Aug 21 '12 at 18:07
  • Right you are Henk. My apologies. See it's not that hard is it. – Mike Perrenoud Aug 21 '12 at 18:24
  • Thank you Henk for your solution. I see now what I was doing wrong. I never even thought about the fact that I was creating the directory with test.doc at the end. – Pallas Aug 21 '12 at 18:29
  • Right you are and I will gladly delete my answer. But I invite you to read your comment a little closer too and consider your tone - `Cosmetic, does not address the problem.` - do you not see how condescending that statement was? I mean it was perceived as a jab by more than one person. Arrogance is not a good trait; though maybe I've misunderstood who you are. Maybe you can't even learn from people like @JonSkeet or @Darin - maybe you know it all my friend. – Mike Perrenoud Aug 21 '12 at 18:48
  • @HenkHolterman - your arrogance will be your downfall. Keep [this post mind](http://stackoverflow.com/faq#etiquette) and I'll be watching for your comments; I will flag them if necessary; good day. – Mike Perrenoud Aug 21 '12 at 18:54
  • 1
    On my profile page, activity-tab, you can find the archive of all my comments. Have fun. – H H Aug 21 '12 at 19:01
1

Try this:

var fileLocation = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "QuickNote");
fileLocation = Path.Combine(fileLocation, "test.doc");
Maarten
  • 22,527
  • 3
  • 47
  • 68
1

If you have C# 4.0 version you can test directly

Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "QuickNote", "test.doc");
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

Path.Combine will remove "\" from the end of the string.

You should use the same method on the second line insead of "+".

Vale
  • 3,258
  • 2
  • 27
  • 43