C# 将文件写入名称递增的路径
在C# 将文件写入名称递增的路径,c#,file,unity3d,C#,File,Unity3d,在Unity3D脚本中,我有以下内容,它只是将文件(图像)写入路径: var path = Application.persistentDataPath + "/ocr.jpg"; FileOperations.Create(blah, path); 当然,在其他地方的静态实用程序中,我实现了这个helper函数,如下所示: public static void Create(byte[] bytes, string path) { var file = File.Open(path,
Unity3D
脚本中,我有以下内容,它只是将文件(图像)写入路径:
var path = Application.persistentDataPath + "/ocr.jpg";
FileOperations.Create(blah, path);
当然,在其他地方的静态
实用程序中,我实现了这个helper函数,如下所示:
public static void Create(byte[] bytes, string path)
{
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
public static void Create(byte[] bytes, string path)
{
if ( !File.Exists(path) )
{
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
else
{
int counter = 1;
path = Application.persistentDataPath + "/ocr" + "_" + counter + ".jpg";
Create(bytes, path);
}
}
我意识到我的实现只会创建一个文件;i、 e.重写一个同名的现有文件(ocr.jpg
),因此我编写了以下代码:
public static void Create(byte[] bytes, string path)
{
if ( !File.Exists(path) )
{
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
else
{
int counter = 1;
path = Application.persistentDataPath + "/ocr" + "_" + counter + ".jpg";
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
}
然后,我意识到每次我运行应用程序时,计数器
都被设置回1
,所以现在我的ocr\u 1.jpg
写得太多了
(不要介意这个半解决方案已经很难看了,因为它带来了一些特定于Unity的东西。我不希望在我的helper实用程序类中包含使用UnityEngine的。)
接下来,我尝试递归地执行此操作,如下所示:
public static void Create(byte[] bytes, string path)
{
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
public static void Create(byte[] bytes, string path)
{
if ( !File.Exists(path) )
{
var file = File.Open(path, FileMode.Create);
using ( var binary = new BinaryWriter(file) )
{
binary.Write(bytes);
file.Close();
}
}
else
{
int counter = 1;
path = Application.persistentDataPath + "/ocr" + "_" + counter + ".jpg";
Create(bytes, path);
}
}
但我收到了一条StackOverFlow
错误消息。我不明白为什么,因为检查应该一直进行到没有同名文件为止,等等
有人能帮助我理解我做错了什么,以及我如何实现我的目标,即运行应用程序任意次数并按顺序创建图像:ocr.jpg
,ocr_1.jpg
,ocr_2.jpg
,ocr_3.jpg
,等等
另外,如果我能找到一种方法,使我的实用程序类不必包含与统一相关的东西,那就太好了;i、 e.只有当您通过已经存在的路径并且
path = Application.persistentDataPath + "/ocr" + "_" + counter + ".jpg";
同样存在,您继续调用Create方法而不增加计数器,因此堆栈溢出。考虑将计数器值传递给递归创建方法,并在每次迭代中更新它,直到它命中一个不存在的文件名。 < P>为什么您得到堆栈溢出是由于方法的填充。在您的代码中,计数器
变量是方法的局部变量,并且从不递增。因此,每次对Create
方法进行递归调用时,都会进行相同的调用(按参数)
作为高级解决方案,您应该做两件事:
- 在整个递归调用过程中跟踪
计数器的值
- 确保每次递归调用
Create
您可以通过将计数器
变量作为全局变量(有时这不是一个好主意)或将其保留为创建
方法参数的一部分来跟踪该变量的状态
我不希望在我的助手实用程序中包含使用UnityEngine的
阶级
你不必这么做。您可以使用包含其命名空间的完全限定名:
UnityEngine.Application.persistentDataPath
要知道的事情很少:
1。读取或写入文件时始终捕获异常
2。不要这样做:Application.persistentDataPath+“/ocr”+““+counter+”.jpg”代码>
将文件名与“+
”连接不是一个好主意,因为它不可跨平台移植。使用路径。组合路径
以下是我将如何实现这一点:
使用PlayerPrefs.SetInt
保存计数器。每次要保存图像时,请使用PlayerPrefs.GetInt
读取计数器,然后使用File.writealBytes
保存图像。如果保存成功,则增加计数器,然后使用PlayerPrefs.SetInt
保存
大致如下:
public static void Create(byte[] dataToSave)
{
//Load old counter
int counter = PlayerPrefs.GetInt("_counter_", 0);
//Concatenate file name
string tempPath = Path.Combine(UnityEngine.Application.persistentDataPath, "ocr");
tempPath = Path.Combine(tempPath, "_");
tempPath = Path.Combine(tempPath, counter.ToString() + ".jpg");
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
//Debug.Log(path);
try
{
File.WriteAllBytes(tempPath, dataToSave);
Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));
//Increment Counter
counter++;
//Save current Counter
PlayerPrefs.SetInt("_counter_", counter);
PlayerPrefs.Save();
}
catch (Exception e)
{
Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/", "\\"));
Debug.LogWarning("Error: " + e.Message);
}
}