4

私は次のコードを持っています:

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

filePathAndNameは次のようになりますC:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.

フォルダは存在しますが、ファイルは存在しません。この場合、XmlTextWriterはファイルを作成する必要がありますが、代わりに。をスローしCould not find part of the pathます。

それはおそらく私がここで忘れている非常に明白なことです、助けてください。

編集:これはパスが実際にどのように見えるかです:

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

そして、これはコードで使用されるURLが生成される方法です:

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

5 に答える 5

3

私はコードを試しましたが、コンストラクターによって次のメッセージArgumentExceptionがスローされます:XmlTextWriter

URI形式はサポートされていません。

次のコードを検討してください。

// 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))
{
    ...
}
于 2013-01-29T14:14:56.467 に答える
1

これを試してください-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();
    }
}
于 2013-01-29T14:13:39.407 に答える
1

ネットワーク上のパス(別名UNCパス)を操作している場合は、Server.MapPathを使用して、UNCパスまたは仮想パスを.NETが理解できる物理パスに変換する必要があります。したがって、ファイルを開いたり、ファイルを作成、更新、削除したり、ディレクトリを開いたり、ネットワークパス上のディレクトリを削除したりするときは、いつでもを使用してServer.MapPathください。

例:

System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));
于 2014-05-02T18:46:08.343 に答える
0

使用する代わりにUri.AbsolutePathあなたは取る必要があります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...
}
于 2013-01-29T14:16:23.623 に答える
0

Assembly.LocationとPath.Combineを使用して、fileNameAndPath変数を作成します。

var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePathAndName = Path.Combine(folder, "ExceptionLog.xml");
于 2013-01-29T14:17:25.297 に答える