C# IOException进程无法访问该文件,因为在使用XDocument时另一进程正在使用该文件
我一直收到一个IOException,它无法访问该文件,因为它正被另一个进程使用。我想做的是每次我查看的文件都会被更改。。它通过TCP/IP以数组的形式发送。我找不到任何关闭XDocument的方法,只是不知道如何修复此错误。。。我用谷歌搜索了一下,还是什么都找不到。任何帮助都将不胜感激 编辑:我用filereader和其他东西找到了其他解决方案。。但使用xdocument时似乎有所不同C# IOException进程无法访问该文件,因为在使用XDocument时另一进程正在使用该文件,c#,xml,ioexception,C#,Xml,Ioexception,我一直收到一个IOException,它无法访问该文件,因为它正被另一个进程使用。我想做的是每次我查看的文件都会被更改。。它通过TCP/IP以数组的形式发送。我找不到任何关闭XDocument的方法,只是不知道如何修复此错误。。。我用谷歌搜索了一下,还是什么都找不到。任何帮助都将不胜感激 编辑:我用filereader和其他东西找到了其他解决方案。。但使用xdocument时似乎有所不同 using System; using System.Collections.Generic; using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//filesystemwatcher
using System.IO;
//tcpip server
using System.Net;
using System.Net.Sockets;
//XML
using System.Xml;
using System.Xml.Linq;
namespace ChampSelect_FileWatcher
{
class Program
{
//TCP IP variables
public static Int32 port;
public static IPAddress localAddr;
public static TcpListener server;
public static TcpClient client;
public static NetworkStream stream;
public static XDocument doc;
public static void Main(string[] args)
{
//LOAD XML FILE
doc = XDocument.Load("C:/Trio Scripts/example.xml");
//OBSERVE FILE
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Trio Scripts";
watcher.Filter = "example.xml";
//watch for changes in LastWrite
watcher.NotifyFilter = NotifyFilters.LastWrite;
//event handler
watcher.Changed += new FileSystemEventHandler(OnChanged);
//Begin watching
watcher.EnableRaisingEvents = true;
//TCP IP SERVER
try
{
Console.WriteLine("Waiting for 99150 to run...\n");
//config TCP stuff
port = 9905;
localAddr = IPAddress.Parse("10.0.0.66");
server = new TcpListener(localAddr, port);
server.Start();
client = server.AcceptTcpClient();
stream = client.GetStream();
Console.WriteLine("Connection to Viz successful!");
Console.WriteLine("***LISTENING FOR CHANGES TO: " + watcher.Filter + "***\n");
}
catch (SocketException z)
{
Console.WriteLine("SocketException: {0}", z);
}
//prevent console from closing
Console.ReadKey();
Console.ReadKey();
Console.ReadKey();
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
//reload xml file
//doc = XDocument.Load("C:/Trio Scripts/example.xml");
//doc.Root.ReplaceWith(XElement.Load("C:/Trio Scripts/example.xml"));
XDocument doc;
using (var reader = XmlReader.Create("C:/Trio Scripts/example.xml"))
{
doc = XDocument.Load(reader);
}
// Nodes in XML
string[] bans = doc.Descendants("ban").OrderBy(element => Int32.Parse(element.Attribute("order").Value)).Select(element => element.Value).ToArray();
// String to send the message on
String sendMsg = "";
// Proceed with reading XML
for (int i = 0; i < bans.Length; i++)
sendMsg += bans[i] + " ";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(sendMsg);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Change detected, sending changes to Viz");
sendMsg = "";
}
}//end class
}//end namespace
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
//文件系统监视程序
使用System.IO;
//tcpip服务器
Net系统;
使用System.Net.Sockets;
//XML
使用System.Xml;
使用System.Xml.Linq;
名称空间选择\u文件监视程序
{
班级计划
{
//TCP IP变量
公共静态Int32端口;
公共静态IP地址localAddr;
公共静态TcpListener服务器;
公共静态TcpClient客户端;
公共静态网络流;
公共静态XDocument文档;
公共静态void Main(字符串[]args)
{
//加载XML文件
doc=XDocument.Load(“C:/Trio Scripts/example.xml”);
//观察文件
FileSystemWatcher watcher=新的FileSystemWatcher();
Path=@“C:\Trio脚本”;
watcher.Filter=“example.xml”;
//注意LastWrite中的更改
watcher.NotifyFilter=NotifyFilters.LastWrite;
//事件处理程序
watcher.Changed+=新文件系统EventHandler(OnChanged);
//开始观看
watcher.EnableRaisingEvents=true;
//TCP IP服务器
尝试
{
Console.WriteLine(“正在等待99150运行…\n”);
//配置TCP文件
端口=9905;
localAddr=IPAddress.Parse(“10.0.0.66”);
服务器=新的TcpListener(localAddr,端口);
server.Start();
client=server.AcceptTcpClient();
stream=client.GetStream();
Console.WriteLine(“连接到Viz成功!”);
Console.WriteLine(“***正在侦听对“+watcher.Filter+”***\n)的更改);
}
捕获(SocketException z)
{
WriteLine(“SocketException:{0}”,z);
}
//防止控制台关闭
Console.ReadKey();
Console.ReadKey();
Console.ReadKey();
}
//定义事件处理程序。
私有静态void OnChanged(对象源、文件系统目标)
{
//重新加载xml文件
//doc=XDocument.Load(“C:/Trio Scripts/example.xml”);
//doc.Root.ReplaceWith(XElement.Load(“C:/Trio Scripts/example.xml”);
XDocument文件;
使用(var reader=XmlReader.Create(“C:/Trio Scripts/example.xml”))
{
doc=XDocument.Load(读卡器);
}
//XML中的节点
string[]bans=doc.substands(“ban”).OrderBy(element=>Int32.Parse(element.Attribute(“order”).Value)).Select(element=>element.Value.ToArray();
//用于发送消息的字符串
字符串sendMsg=“”;
//继续阅读XML
for(int i=0;i
您仍然可以使用XDocument
和XmlReader
:
XDocument doc;
using (var reader = XmlReader.Create("C:/Trio Scripts/example.xml"))
{
doc = XDocument.Load(reader);
}
当读取器的using
块完成时,应关闭文件句柄
更新:
可能XmlReader.Create(string)
的行为没有以最简单的方式打开文件。如果这就是导致异常的原因,请尝试以下更明确的代码,指定文件权限:
XDocument doc;
using (var stream = File.Open("C:/Trio Scripts/example.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = XmlReader.Create(stream))
{
doc = XDocument.Load(reader);
}
您仍然可以使用
XDocument
和XmlReader
:
XDocument doc;
using (var reader = XmlReader.Create("C:/Trio Scripts/example.xml"))
{
doc = XDocument.Load(reader);
}
当读取器的using
块完成时,应关闭文件句柄
更新:
可能XmlReader.Create(string)
的行为没有以最简单的方式打开文件。如果这就是导致异常的原因,请尝试以下更明确的代码,指定文件权限:
XDocument doc;
using (var stream = File.Open("C:/Trio Scripts/example.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = XmlReader.Create(stream))
{
doc = XDocument.Load(reader);
}
问题是,每当文件发生更改时,都会出现多个更改事件 在读取更改的文件之前,您应该等待一段时间 您还应该使用
e.FullPath
并检查
e.ChangeType == WatcherChangeTypes.Changed
如果无法打开该文件,则应稍后重试。问题在于,每当文件发生更改时,都会出现多个更改事件 在读取更改的文件之前,您应该等待一段时间 您还应该使用
e.FullPath
并检查
e.ChangeType == WatcherChangeTypes.Changed
如果无法打开该文件,应稍后再试。是否在代码的任何位置使用
“C:/Trio Scripts/example.xml”
文件?如果是,则在文件更改时调用tooFileSystemWatcher.OnChanged。哪个文件?和example.xml有什么关系?更新了代码,这样你们就可以在代码的任何地方看到你们使用的所有东西了?如果是,则在文件更改时调用tooFileSystemWatcher.OnChanged。哪个文件?与example.xml有何关系?更新了代码,这样大家就可以看到所有东西。作为一名.net开发人员,您应该非常熟悉using语句及其多种用途。^torobot我们是C新手,只是学习w