如何用C#删除文件的最后一个字符
How to delete the last character of a file with C#
你好我是 C# 的初学者,我想删除文件的最后一个字符以手动将 JSON 对象注入此文件(我知道这不是最好的方法),所以我可以获得正确的格式 我尝试了多种方法,例如打开文件、操作字符串(删除最后一个字符)以及当我尝试替换同一文件中的文本时出现 IOException: The process cannot access the file 'file path' because it is being used by another process
或 [=13= 等错误]
我给你看代码:
StoreLogs Log = new StoreLogs()
{
Id = ID,
DateTime = dateT,
TaskName = task,
SrcAddress = srcPath,
DstAddress = path,
FileSize = DirSize(new DirectoryInfo(srcPath)),
DelayTransfer = ts.Milliseconds,
};
// Record JSON data in the variable
string strResultJson = JsonConvert.SerializeObject(Log);
// Show the JSON Data
// Console.WriteLine(strResultJson);
// Write JSON Data in another file
string MyJSON = null;
string strPath = @"C:\Users\ASUS\Desktop\Backup\logs\log.json";
if (File.Exists(strPath))
{
//FileInfo table = new FileInfo(strPath);
//string strTable = table.OpenText().ReadToEnd();
//string erase = strTable.Remove(strTable.LastIndexOf(']'));
//Console.WriteLine(erase);
//StreamReader r1 = new StreamReader(strPath);
//string strTable = r1.OpenText().ReadToEnd();
//string erase = strTable.Remove(strTable.LastIndexOf(']'));
//r1.Close();
using (StreamReader sr = File.OpenText(strPath))
{
string table = sr.ReadToEnd();
string erase = table.Remove(table.LastIndexOf(']'));
sr.Close();
File.WriteAllText(strPath, erase);
}
//MyJSON = "," + strResultJson;
//File.AppendAllText(strPath, MyJSON + "]");
//Console.WriteLine("The file exists.");
}
else if (!File.Exists(strPath))
{
MyJSON = "[" + strResultJson + "]";
File.WriteAllText(strPath, MyJSON);
Console.WriteLine("The file doesn't exists.");
}
else
{
Console.WriteLine("Error");
}
// End
Console.WriteLine("JSON Object generated !");
Console.ReadLine();
这就是我想要的结果:
[{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0},{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0},{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0}]
编辑:
谢谢大家的建议
解法:
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.ReadWrite);
fs.SetLength(fs.Length - 1);
fs.Close();
它说的是 运行 应用程序的用户拒绝访问路径 (C:\Users\ASUS\Desktop\Root)。例如:如果您是 Visual studio 的 运行,在 user1 windows 上登录,则 user1 应该对该根文件夹具有适当的权限。如果代码本身是 运行 (exe),则检查调用该 exe 的用户的访问权限。
在您发布的代码示例中,您打开了一个流来读取文件。退出块后,using 块将处理流。您正在尝试写入文件,而读取流仍在访问它(读取流仍然存在)。你基本上已经打开了文件,你从中读取,并试图在保持打开状态的同时写回它。这是一个问题的原因是您没有使用流来写入。因此,您的第二个写入进程无法访问该文件。我看到你在写入之前关闭了流,但我敢打赌它仍然保持引用打开。
我会试试这个方法:
How to both read and write a file in C#
根据您发布的错误看来:
也许你让一些流打开指向你想编辑的文件,使用 'using' 语句来避免这种情况 (see this link for more info)
您在没有所需权限的情况下尝试访问文件(您不是系统管理员或文件是只读的),请尝试更改文件发布或将其设置为可写 (see this link for mor info about the UnauthorizedAccessException exception)
希望对您有所帮助!
你好我是 C# 的初学者,我想删除文件的最后一个字符以手动将 JSON 对象注入此文件(我知道这不是最好的方法),所以我可以获得正确的格式 我尝试了多种方法,例如打开文件、操作字符串(删除最后一个字符)以及当我尝试替换同一文件中的文本时出现 IOException: The process cannot access the file 'file path' because it is being used by another process
或 [=13= 等错误]
我给你看代码:
StoreLogs Log = new StoreLogs()
{
Id = ID,
DateTime = dateT,
TaskName = task,
SrcAddress = srcPath,
DstAddress = path,
FileSize = DirSize(new DirectoryInfo(srcPath)),
DelayTransfer = ts.Milliseconds,
};
// Record JSON data in the variable
string strResultJson = JsonConvert.SerializeObject(Log);
// Show the JSON Data
// Console.WriteLine(strResultJson);
// Write JSON Data in another file
string MyJSON = null;
string strPath = @"C:\Users\ASUS\Desktop\Backup\logs\log.json";
if (File.Exists(strPath))
{
//FileInfo table = new FileInfo(strPath);
//string strTable = table.OpenText().ReadToEnd();
//string erase = strTable.Remove(strTable.LastIndexOf(']'));
//Console.WriteLine(erase);
//StreamReader r1 = new StreamReader(strPath);
//string strTable = r1.OpenText().ReadToEnd();
//string erase = strTable.Remove(strTable.LastIndexOf(']'));
//r1.Close();
using (StreamReader sr = File.OpenText(strPath))
{
string table = sr.ReadToEnd();
string erase = table.Remove(table.LastIndexOf(']'));
sr.Close();
File.WriteAllText(strPath, erase);
}
//MyJSON = "," + strResultJson;
//File.AppendAllText(strPath, MyJSON + "]");
//Console.WriteLine("The file exists.");
}
else if (!File.Exists(strPath))
{
MyJSON = "[" + strResultJson + "]";
File.WriteAllText(strPath, MyJSON);
Console.WriteLine("The file doesn't exists.");
}
else
{
Console.WriteLine("Error");
}
// End
Console.WriteLine("JSON Object generated !");
Console.ReadLine();
这就是我想要的结果:
[{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0},{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0},{"Id":"8484","DateTime":"26 novembre 2019 02:33:35 ","TaskName":"dezuhduzhd","SrcAddress":"C:\Users\ASUS\Desktop\Root","DstAddress":"C:\Users\ASUS\Desktop\Backup","FileSize":7997832.0,"DelayTransfer":0.0}]
编辑: 谢谢大家的建议
解法:
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.ReadWrite);
fs.SetLength(fs.Length - 1);
fs.Close();
它说的是 运行 应用程序的用户拒绝访问路径 (C:\Users\ASUS\Desktop\Root)。例如:如果您是 Visual studio 的 运行,在 user1 windows 上登录,则 user1 应该对该根文件夹具有适当的权限。如果代码本身是 运行 (exe),则检查调用该 exe 的用户的访问权限。
在您发布的代码示例中,您打开了一个流来读取文件。退出块后,using 块将处理流。您正在尝试写入文件,而读取流仍在访问它(读取流仍然存在)。你基本上已经打开了文件,你从中读取,并试图在保持打开状态的同时写回它。这是一个问题的原因是您没有使用流来写入。因此,您的第二个写入进程无法访问该文件。我看到你在写入之前关闭了流,但我敢打赌它仍然保持引用打开。
我会试试这个方法: How to both read and write a file in C#
根据您发布的错误看来:
也许你让一些流打开指向你想编辑的文件,使用 'using' 语句来避免这种情况 (see this link for more info)
您在没有所需权限的情况下尝试访问文件(您不是系统管理员或文件是只读的),请尝试更改文件发布或将其设置为可写 (see this link for mor info about the UnauthorizedAccessException exception)
希望对您有所帮助!