简体   繁体   中英

Saving file into Path.Combine directory

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM