0

Alright so I've decided to create my first GUI Address book and I've been following a tutorial on Youtube that allows us to create an Address Book that is suppose to store the information entered into the text boxes as an xml but I've been having trouble.

So when the form loads, it's suppose to check for a folder named "Address Book" and if it's not there, create it. Then check if a document is in there, if not, it creates it. But for some reason the folder does not get created, even though it doesn't exist, same with the xml file.

private void Form1_Load(object sender, EventArgs e)
{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    if(!Directory.Exists(path + "\\Address Book"))
        Directory.CreateDirectory(path + "\\Address Book");
    if (!File.Exists(path + "\\Address Book\\settings.xml"))
        {
            XmlTextWriter xW = new XmlTextWriter(path + "\\Address Book\\settings.xml", Encoding.UTF8);
            xW.WriteStartElement("People");
            xW.WriteEndElement();
            xW.Close();
        }

Can anyone point out my mistake? The original path was SpecialFolder.ApplicationData but I wanted to use desktop because looking for the folder would be a click away.

user1580598
  • 53
  • 1
  • 2
  • 9

1 Answers1

4

The sounds pretty much like Read/Write permission access issue.

Do not use Desktop like file/directory write destination, but use folders where your OS User, so the application too, is guranteed to have relative permissions. So those folders which you saw in tutorial. Especially latest WinOS es are very rigid on this kind of stuff.

If it frustraits you, to navigate to those folders every time, create a link to that folder on your Desktop. Fast and easy.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • So create a folder inside of the Public folder? – user1580598 Aug 29 '12 at 20:10
  • Oh wait....I think I know what the problem is...let me check... – user1580598 Aug 29 '12 at 20:12
  • Oh god...I am so stupid! I am using a virtual version of Visual Studio 2010 and the reason the files weren't creating on my desktop is because I was creating them on the virtual desktop and search for them on my computer. So sorry everyone! – user1580598 Aug 29 '12 at 20:15
  • 1
    @user1580598: even if you're able to do that on your machine, for some security luck, do not do that. Do not write code that will fail (for sure) on client machine with a slightly different configuration then yours. Good luck. – Tigran Aug 29 '12 at 20:21