user726720
user726720

Reputation: 1237

XmlTextWriter - Access to the path is denied

This seems to be quite simple but I can't seem to figure out the problem

public static string destinationFile;

[STAThread]
private static void Main(string[] args)
{
    //doing something and then calling convert method
}

private static void convert(object source, FileSystemEventArgs f)
{
    if (check(FileName))
    {
        //doing something

        XmlTextWriter myWriter = new XmlTextWriter(destinationFile, null);

        //doing something
    }
}

private static bool check(string filename)
{
    //check the file and return a boolean result
    if (sometest)
    {
        destinationFile = @"d:/GS";
        return true;
    }

    return false;
}

When I run this I get:

The process failed:
System.UnauthorizedAccessException: Access to the path is denied

May I know where I'm going wrong.

Upvotes: 2

Views: 2531

Answers (3)

user3333735
user3333735

Reputation: 57

To start debugging this I would probably put some very simple xml located at that same location

var xml = "<?xml version=\"1.0\"?><hello><world>hello world</world></hello>";
 XDocument xdoc = XDocument.Parse(xml);
 xdoc.Save("d:\test.xml");

then try to read in the new path see if your xml reader can access the test file

Upvotes: 0

GvS
GvS

Reputation: 52518

You are trying to write to a file, that is actually already a folder on your filesystem.

In your check method, you set destinationFile to "D:\GS", and later you use destinationFile as the target of your XmlTextWriter.

Probably you want something as:

  XmlTextWriter myWriter = new XmlTextWriter(Path.Combine(destinationFile, FileName), null);

Upvotes: 4

Jens H
Jens H

Reputation: 4632

As misleading as the exception message is, this may indicate that the file is hidden.

You can check this via right-click from the Windows Explorer => Properties. Is the the "Hidden" CheckBox activated? If yes, uncheck it and try again.

You can also remove it in the code of your application (if the business logic allows for this). You can find example code for this here: How do I write to a hidden file?

Upvotes: 0

Related Questions