unity dll文件删除文件写异常
Unity dll file delete file and write exception
我正在尝试使用文件来读写我的数据,这是我在 dll 文件中编写的代码
public static void WriteFile(string fileName, string writeline)
{
bool checker = false;
#if UNITY_WEBPLAYER
checker = true;
#endif
if (checker)
{
return;
}
string pathname = Application.persistentDataPath + "/" + fileName;
if (File.Exists(pathname))
{
Debug.Log("userdata already exists.");
File.Delete(pathname);
}
// Create the file.
using (FileStream fs = File.Create(pathname))
{
Byte[] info = new UTF8Encoding(true).GetBytes(writeline);
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
当我尝试写入文件并且该文件存在时会抛出此异常,但如果我再次写入则不会出现异常。我猜我第一次调用这个文件时文件被成功删除 function.But 我需要解决第一次写文件异常。
IOException: Sharing violation on path C:/Users/player/AppData/LocalLow/DefaultCompany/Testing Unity Project/UserInfo.txt
System.IO.File.Delete (System.String path) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:179)
FileControl.WriteFile (System.String fileName, System.String writeline)
Test+<LoginClientTest>d__0.MoveNext ()
这样试试:
// Exclude File.DeleteCode
using (FileStream fs = File.Create(pathname))
{
Byte[] info = new UTF8Encoding(true).GetBytes(writeline);
// Add some information to the file.
fs.Write(info, 0, info.Length);
fs.Close(); // Close the stream
}
或尝试使用 StreamWriter
.
写入文件
我正在尝试使用文件来读写我的数据,这是我在 dll 文件中编写的代码
public static void WriteFile(string fileName, string writeline)
{
bool checker = false;
#if UNITY_WEBPLAYER
checker = true;
#endif
if (checker)
{
return;
}
string pathname = Application.persistentDataPath + "/" + fileName;
if (File.Exists(pathname))
{
Debug.Log("userdata already exists.");
File.Delete(pathname);
}
// Create the file.
using (FileStream fs = File.Create(pathname))
{
Byte[] info = new UTF8Encoding(true).GetBytes(writeline);
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
当我尝试写入文件并且该文件存在时会抛出此异常,但如果我再次写入则不会出现异常。我猜我第一次调用这个文件时文件被成功删除 function.But 我需要解决第一次写文件异常。
IOException: Sharing violation on path C:/Users/player/AppData/LocalLow/DefaultCompany/Testing Unity Project/UserInfo.txt
System.IO.File.Delete (System.String path) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:179)
FileControl.WriteFile (System.String fileName, System.String writeline)
Test+<LoginClientTest>d__0.MoveNext ()
这样试试:
// Exclude File.DeleteCode
using (FileStream fs = File.Create(pathname))
{
Byte[] info = new UTF8Encoding(true).GetBytes(writeline);
// Add some information to the file.
fs.Write(info, 0, info.Length);
fs.Close(); // Close the stream
}
或尝试使用 StreamWriter
.