Couldn't find part of the path?

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 filenamePathAndName will be C:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.

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

.

Maybe something very obvious that I forgot here, please help.

Edit: this is how the path looks like:

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

      

And this is how the URL is created and used in the code:

 (new System.Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "\\ExceptionLog.xml")).AbsolutePath

      

+3


source to share


5 answers


I have tried the code ArgumentException

called by the constructor XmlTextWriter

with this post:

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))
{
    ...
}

      

+3


source


Try this - add @ in front of the filename



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();
    }
}

      

+1


source


If you are interacting with a contour on a network (aka UNC path), you should use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you open files, create, update and delete files, open directories and delete directories over a network path, use Server.MapPath

.

Example:

System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));

      

+1


source


Instead, Uri.AbsolutePath

you should takePath.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...
}

      

0


source


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

var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePathAndName = Path.Combine(folder, "ExceptionLog.xml");

      

0


source







All Articles