0

My problem should be plain and simple to solve, but google is not helping me today.

I need to read/write a configuration file (config.xml) and, as i see so much problems with permissions with special folders, i decided for myDocuments.

Now, from File system (Setup), I added a custom special folder (myDocuments)
added a subfolder (g1OKweb) inside myDocuments
added the file (config.xml) inside g1OKweb

What I expect, reading around, is that during the installation g1OKweb should be created if not existing or older, and the same for config.xml, but it isn't.

Does someone have any clue?

Thanks in advance

  • Would it not be easier to store the configuration file in the same folder as the executable? – DWRoelands Jun 17 '13 at 14:37
  • @DWRoelands: Only if you could guarantee that all the users of your app have full admin rights, and even then it's strongly discouraged by Microsoft. Luigi's approach is correct. – Christian Hayter Jun 17 '13 at 14:43

1 Answers1

0

Use Directory.CreateDirectory to create the directory before attempting to access the file. This will automatically create all parts of the path that do not yet exist. If the full path already exists, it will do nothing.

When opening the file, use a FileStream constructor overload that allows you to specify FileMode.OpenOrCreate. This will succeed regardless of whether the file already exists or not.

When you have opened the file, check to see if it is empty before parsing it. If it is empty, insert your XML root element first.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • This is what i did formerly, but now my aim is to create everything during the installation process. – Luigi Freguglia Jun 17 '13 at 15:14
  • In that case, you need to include the empty config file in your installer, make sure it's installed to the correct place, and that it does not overwrite any existing file. How you do all that depends on which installer technology you are using. – Christian Hayter Jun 17 '13 at 15:28
  • I'm using the Visual Studio included Setup Project. – Luigi Freguglia Jun 17 '13 at 18:49
  • But should not be the default behavior to copy the things I include in the File System tab? – Luigi Freguglia Jun 17 '13 at 18:51
  • It sounds like you are already on the right track. I'm confused now. What behaviour are you seeing, and how does it differ from what you want? – Christian Hayter Jun 17 '13 at 20:05
  • I expect for the folder and the file to be copied in myDocuments during the installation, but after I run the setup nothing happens in myDocuments. – Luigi Freguglia Jun 17 '13 at 20:11
  • Visual Studio setup projects are full of annoying limitations and sometimes it's hard to get what you want. If you give me more details about the exact contents of your File System tab, then I *might* be able to help. Personally, I would go back to the strategy of creating the file on first use. You know exactly what is going on in that code, and it's easier to test as well. – Christian Hayter Jun 18 '13 at 07:51
  • Maybe your is the best solution, i'll follow your advice. Thank you, Christian, for the suport! – Luigi Freguglia Jun 18 '13 at 20:46