4

I have the following code :

fileinfo = new FileInfo(filePathAndName);

if (!fileinfo.Exists)
{
    using (xmlWriter = new XmlTextWriter(filePathAndName, System.Text.Encoding.UTF8))
    {
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("root");
        xmlWriter.WriteStartElement("objects");
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Close();
    }
}

The filePathAndName will be C:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.

The folder does exists but the file does not. XmlTextWriter should in this case create the file but instead it throws Could not find part of the path.

It's probably something very obvious I have forgotten here, please help.

Edit : This is how the path really looks like :

C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin

And this is how the URL that is used in the code is generated :

 (new System.Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "\\ExceptionLog.xml")).AbsolutePath
David Rogers
  • 2,601
  • 4
  • 39
  • 84
Banshee
  • 15,376
  • 38
  • 128
  • 219

5 Answers5

3

I've tried the code, ArgumentException is thrown by XmlTextWriter constructor with this message:

the URI formats are not supported.

Consider the following code:

// Get the path to assembly directory.
// There is a lot of alternatives: http://stackoverflow.com/questions/52797/
var assemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
var directoryPath = Path.GetDirectoryName(assemblyPath);

// Path to XML-file.
var filePath = Path.Combine(directoryPath, "ExceptionLog.xml");

using (var xmlTextWriter = new XmlTextWriter(filePath, Encoding.UTF8))
{
    ...
}
1

Try this -- add @ before the filePathAndName

string filePathAndName = @"C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin\text.xml";

FileInfo fileinfo = new FileInfo(filePathAndName);

if (!fileinfo.Exists)
{
    using (XmlTextWriter xmlWriter = new XmlTextWriter(filePathAndName, System.Text.Encoding.UTF8))
    {
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("root");
        xmlWriter.WriteStartElement("objects");
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Close();
    }
}
Naga Sandeep
  • 1,421
  • 2
  • 13
  • 26
1

If you are interacting with a path on a network (aka UNC path), you have to use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you're opening files, creating, updating and deleting files, opening directories and deleting directories on a network path, use Server.MapPath.

Example:

System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
0

Instead of using Uri.AbsolutePath you should take Path.Combine()

var filepath = @"C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin"
var filename = Path.Combine(filepath, "ExceptionLog.xml");

var fileInfo = new FileInfo(filename);

if(!fileInfo.Exists)
{
    //ToDo: call xml writer...
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
0

Use Assembly.Location and Path.Combine to form your fileNameAndPath variable:

var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePathAndName = Path.Combine(folder, "ExceptionLog.xml");
RogerN
  • 3,761
  • 11
  • 18