C# XmlSerializer访问被拒绝
如何使上述代码停止抛出InvalidOperationException 完整的错误消息是: 无法生成临时类(结果=1)。 错误CS0016:无法写入输出文件“c:\Users[MYUSERNAME]\AppData\Local\Temp\czdgjjs0.dll”--“访问被拒绝。” 我不知道如何避开这个错误 我正在尝试序列化我的级别类,如下所示:C# XmlSerializer访问被拒绝,c#,serialization,C#,Serialization,如何使上述代码停止抛出InvalidOperationException 完整的错误消息是: 无法生成临时类(结果=1)。 错误CS0016:无法写入输出文件“c:\Users[MYUSERNAME]\AppData\Local\Temp\czdgjjs0.dll”--“访问被拒绝。” 我不知道如何避开这个错误 我正在尝试序列化我的级别类,如下所示: public void SerializeObject(string filename, T data) { //
public void SerializeObject(string filename, T data)
{
// Get the path of the save game
string fullpath = filename;
// Open the file, creating it if necessary
//if (container.FileExists(filename))
// container.DeleteFile(filename);
FileStream stream = (FileStream)File.Open(fullpath, FileMode.OpenOrCreate);
try
{
// Convert the object to XML data and put it in the stream
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(stream, data); //Thrown HERE
}
finally
{
// Close the file
stream.Close();
}
}
[可序列化]
公共类级别:ISerializable
{
公共字符串名称{get;set;}
public int BestTime{get;set;}//以秒为单位
公共列表级别块{get;set;}
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共级别()
{
}
公共级别(SerializationInfo信息、StreamingContext ctxt)
{
this.Name=(String)info.GetValue(“Name”,typeof(String));
this.BestTime=(int)info.GetValue(“BestTime”,typeof(int));
this.levelBlocks=(List)info.GetValue(“Blocks”,typeof(List));
this.Width=(int)info.GetValue(“Width”,typeof(int));
this.Height=(int)info.GetValue(“Height”,typeof(int));
}
public void GetObjectData(SerializationInfo信息,StreamingContext ctxt)
{
info.AddValue(“Name”,this.Name);
info.AddValue(“BestTime”,此为.BestTime);
info.AddValue(“Blocks”,此为.levelBlocks);
info.AddValue(“宽度”,此为.Width);
info.AddValue(“高度”,即此高度);
}
}
我的blocks类是以类似的方式实现的,它只保存一个保存的位置向量
下面是我的保存方法:
[Serializable]
public class Level : ISerializable
{
public string Name { get; set; }
public int BestTime { get; set; } //In seconds
public List<Block> levelBlocks { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Level()
{
}
public Level(SerializationInfo info, StreamingContext ctxt)
{
this.Name = (String)info.GetValue("Name", typeof(String));
this.BestTime = (int)info.GetValue("BestTime", typeof(int));
this.levelBlocks = (List<Block>)info.GetValue("Blocks", typeof(List<Block>));
this.Width = (int)info.GetValue("Width", typeof(int));
this.Height = (int)info.GetValue("Height", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Name", this.Name);
info.AddValue("BestTime", this.BestTime);
info.AddValue("Blocks", this.levelBlocks);
info.AddValue("Width", this.Width);
info.AddValue("Height", this.Height);
}
}
publicstaticvoidsave()
{
string filename=“saved.xml”;
Level to save=新级别();
toSave.levelBlocks=新列表();
//TODO:构建以保存
toSave.Name=“这是一个级别!”;
toSave.BestTime=0;
foreach(EntityController.Entities中的实体e)
{
如果(e是块)
{
保存levelBlocks.Add((Block)e);
如果(e.Position.X>toSave.Width)
toSave.Width=(int)e.Position.X;
如果(如位置Y>保存高度)
toSave.Height=(int)e.Position.Y;
}
}
serializer.SerializeObject(文件名,toSave);
}
我的程序是一个XNA游戏。这里接受的答案可能会为您提供一个合理的解决方案
他们没有建议的一种可能性是:如果您使用的是ASP.NET,请更改web.config中的临时目录。检查compilation元素的tempDirectory属性(此处为info),并更改为ASP.NET进程确实可以访问的位置
不过,最终的问题是,执行序列化的进程需要生成一些代码并将其写入磁盘,并且没有权限。您可以授予该进程权限,将位置更改为它有权限的位置,或使用sgen.exe,这取决于最适合您的情况。此处接受的答案可能会为您提供合理的解决方案
他们没有建议的一种可能性是:如果您使用的是ASP.NET,请更改web.config中的临时目录。检查compilation元素的tempDirectory属性(此处为info),并更改为ASP.NET进程确实可以访问的位置
不过,最终的问题是,执行序列化的进程需要生成一些代码并将其写入磁盘,并且没有权限。您可以授予该进程权限,将位置更改为它有权限的位置,或使用sgen.exe,这取决于最适合您的情况。使用COMODO antivirus并获取CS0016错误
打开COMODO命令窗口(主窗口),并检查沙箱。如果您的应用程序被列为标记为“受限”的应用程序,只需右键单击并从弹出窗口中选择选项,即可将您的应用程序添加为受信任的应用程序。或者只是卸载COMODO并重新启动。这应该可以解决CS0016错误的问题。使用COMODO antivirus并获得CS0016错误
打开COMODO命令窗口(主窗口),并检查沙箱。如果您的应用程序被列为标记为“受限”的应用程序,只需右键单击并从弹出窗口中选择选项,即可将您的应用程序添加为受信任的应用程序。或者只需卸载COMODO并重新启动。这应该可以解决CS0016错误的问题。根据Sheldon的回答,您能说出如何调用
序列化对象吗,如果您使用的是ASP.NET,请确保您的IIS工作进程具有访问tempDirectory的权限,否则会将其预设为专用文件夹。您能否按照Sheldon的回答说明如何调用Serialized Object
,如果您使用的是ASP.NET,请确保您的IIS工作进程具有访问临时目录的权限,否则会将临时目录预先设置为专用文件夹。进一步调查后,该错误变得更加有趣。每个用户(我是唯一一个)都可以完全访问我的临时文件夹,那么您/我如何获得被拒绝的权限?我还尝试在VS之外以管理员身份运行二进制文件,但没有效果。我的程序是一个XNA游戏,如果这有什么不同的话。经过进一步调查,错误变得更加有趣。每个用户(我是唯一一个)都可以完全访问我的临时文件夹,那么您/我如何获得被拒绝的权限?我还尝试在VS之外以管理员身份运行二进制文件,但没有效果。如果有什么不同的话,我的程序就是XNA游戏。
public static void Save()
{
string filename = "saved.xml";
Level toSave = new Level();
toSave.levelBlocks = new List<Block>();
//TODO: build toSave
toSave.Name = "This is a level!";
toSave.BestTime = 0;
foreach (Entity e in EntityController.Entities)
{
if (e is Block)
{
toSave.levelBlocks.Add((Block)e);
if (e.Position.X > toSave.Width)
toSave.Width = (int)e.Position.X;
if (e.Position.Y > toSave.Height)
toSave.Height = (int)e.Position.Y;
}
}
serializer.SerializeObject(filename, toSave);
}