2

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.

Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
user726720
  • 1,127
  • 7
  • 25
  • 59

3 Answers3

4

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);
GvS
  • 52,015
  • 16
  • 101
  • 139
  • This didn't work exactly but inspired from this view I figured out my problem. I was using the variables wrong. I was using fullpath instead of the name, which was creating a problem. But thanks for the solution. – user726720 Feb 21 '14 at 05:13
0

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?

Community
  • 1
  • 1
Jens H
  • 4,590
  • 2
  • 25
  • 35
0

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