Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top