c# 增加判断,如果文件正在被使用,就主程序暂停等待写入结束 参考 public static void SaveLog(string content, string prefix) { Console.WriteLine("-------------------------------"); Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); Console.WriteLine("-------------------------------"); if (!Directory.Exists(_LogPath)) { Directory.CreateDirectory(_LogPath); } string s = $"{DateTime.Now} ------------------------------------------------------------\r\n{content}\r\n\r\n"; AppendTxt(s, string.Format("{0}{1}{2}.txt", _LogPath, (prefix + "_"), DateTime.Now.ToString("yyyyMMdd"))); } public static void AppendTxt(string s, string fileName) { try { FileStream fs;// = new FileStream(fileName, FileMode.Append,FileAccess.ReadWrite); if (File.Exists(fileName)) { fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite); fs.Position = fs.Length; } else { fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); } byte[] content = Encoding.GetEncoding("UTF-8").GetBytes(s); fs.Write(content, 0, content.Length); fs.Close(); } catch (Exception) { } }
时间: 2023-12-16 19:06:21 浏览: 48
可以使用 `FileShare.None` 参数来打开文件,以确保文件不被其他程序占用。在文件被占用时,程序可以使用 `Thread.Sleep()` 方法暂停一段时间,然后再尝试重新打开文件。
以下是修改后的代码:
```
public static void SaveLog(string content, string prefix)
{
Console.WriteLine("-------------------------------");
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("-------------------------------");
if (!Directory.Exists(_LogPath))
{
Directory.CreateDirectory(_LogPath);
}
string s = $"{DateTime.Now} ------------------------------------------------------------\r\n{content}\r\n\r\n";
AppendTxt(s, string.Format("{0}{1}{2}.txt", _LogPath, (prefix + "_"), DateTime.Now.ToString("yyyyMMdd")));
}
public static void AppendTxt(string s, string fileName)
{
try
{
FileStream fs;
int maxTries = 10;
int tries = 0;
while (true)
{
try
{
fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None);
break;
}
catch (IOException)
{
tries++;
if (tries >= maxTries)
{
throw;
}
Thread.Sleep(1000);
}
}
byte[] content = Encoding.GetEncoding("UTF-8").GetBytes(s);
fs.Write(content, 0, content.Length);
fs.Close();
}
catch (Exception)
{
}
}
```
在使用 `FileStream` 打开文件时,使用了 `FileMode.Append` 参数,以确保新写入的内容会被追加到文件末尾而不是覆盖原有内容。同时,使用了 `FileAccess.Write` 参数,以确保文件可以被写入。
在打开文件时,使用了 `FileShare.None` 参数,以确保文件不被其他程序占用。当文件被占用时,程序会尝试重新打开文件,最多尝试 10 次,每次尝试暂停 1 秒钟。如果尝试次数超过了最大尝试次数,程序会抛出异常。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)