2

Could someone advise of how can I save a file into Path.Combine directory? Please find some of my code below.

Creation of directory:

string wholesalePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), mainFolder, wholesaleFolder);
Directory.CreateDirectory(wholesalePath);

I have also specified a file name which should be used.

 string fileName = "xmltest.xml";

Then I have re-created a "wholesalePath" to include file name:

wholesalePath = Path.Combine(wholesalePath, fileName);

Few simple lines of code that gets executed:

        XmlDocument doc = new XmlDocument();
        string oneLine = "some text";
        doc.Load(new StringReader(oneLine));
        doc.Save(fileName);
        Console.WriteLine(doc);

The problem I am having is that when I use doc.Save(fileName)then I am getting file in VisualStudio project directories which is wrong directory.

However when I am using doc.Save(wholesalePath)then file that should be created "xmltest.xml" is actually created as another directory within "wholesalePath".

I would be grateful for any suggestions.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
A.Rosso
  • 77
  • 3
  • 11
  • Use the debugger and inspect the value of `wholesalePath` on the `doc.Save(fileName);` line. Or just step through the whole thing with the debugger and watch what's happening at each step. – itsme86 May 12 '18 at 17:24
  • Please create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that reproduces the problem. It's very likely that you'll even find the mistake yourself while doing so. – 41686d6564 stands w. Palestine May 12 '18 at 17:38
  • You should be calling `doc.Save(wholesalePath)`. The `fileName` variable is just the file name, with no path, so it will get saved into the current folder. – Jack A. May 12 '18 at 17:45
  • @JackA. I was going o say that, but the OP said that using `doc.Save(wholesalePath)` creates a directory _(somehow!!)_. So, we need an MCVE, really. – 41686d6564 stands w. Palestine May 12 '18 at 17:50
  • @AhmedAbdelhameed I suspect that's because he appended the file name to `wholesalePath` before calling `Directory.CreateDirectory`. The `XmlDocument.Save` method does not create a folder – Jack A. May 12 '18 at 18:00

2 Answers2

2

As said in the comments, you need to use wholesalePath to create the directory before appending fileName. You need to use wholesalePath after appending fileName to save the file. I tested the following code and it works as expected:

void Main()
{
    string mainFolder = "StackOverflow";
    string wholesaleFolder = "Test";
    string wholesalePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), mainFolder, wholesaleFolder);
    Directory.CreateDirectory(wholesalePath);
    string fileName = "xmltest.xml";
    wholesalePath = Path.Combine(wholesalePath, fileName);
    XmlDocument doc = new XmlDocument();
    string oneLine = "<root></root>";
    doc.Load(new StringReader(oneLine));
    doc.Save(wholesalePath);
}

It creates a file named xmltest.xml in a desktop folder named StackOverflow\Test.

That will work, but I would recommend creating separate variables for the folder and the file path. This will make the code clearer, since each variable will only have one purpose, and will make such errors less likely. For example:

void Main()
{
    string mainFolder = "StackOverflow";
    string wholesaleFolder = "Test";
    string fileName = "xmltest.xml";
    string destinationFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), mainFolder, wholesaleFolder);
    string destinationFilePath = Path.Combine(destinationFolder, fileName);

    Directory.CreateDirectory(destinationFolder);
    XmlDocument doc = new XmlDocument();
    string oneLine = "<root></root>";
    doc.Load(new StringReader(oneLine));
    doc.Save(destinationFilePath);
}
Jack A.
  • 4,245
  • 1
  • 20
  • 34
0

Good spot guys,

Thanks a lot for your feedback and quick turn over. As you have mentioned I was changing the action for the wholesalePath before if was actually created.

void Main()
{
    string mainFolder = "StackOverflow";
    string wholesaleFolder = "Test";
    string wholesalePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), mainFolder, wholesaleFolder);

    wholesalePath = Path.Combine(wholesalePath, fileName);

    Directory.CreateDirectory(wholesalePath);
    string fileName = "xmltest.xml";
    XmlDocument doc = new XmlDocument();
    string oneLine = "<root></root>";
    doc.Load(new StringReader(oneLine));
    doc.Save(wholesalePath);
}

Now as I have changed execution sequence to Directory.CreateDirectory(wholesalePath) as first and then the wholesalePath = Path.Combine(wholesalePath, fileName) everything works like a charm. Thanks a lot again for your halp.

Much appreciated.

A.Rosso
  • 77
  • 3
  • 11