为什么我得到另一个进程错误使用的文件?
Why am I getting the file used by another process error?
当我调用 Generate
函数时,它不会创建 StreamWriter
对象,而是抛出一个异常:
file used by another process
但是文件没有打开,这是第一个使用它的流。
public static string GetWindowsUserName()
{
string st = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
bool Condition = false;
foreach (char ch in st)
{
if (ch == '\')
Condition = true;
}
if (Condition)
{
string[] stArr = st.Split('\');
st = stArr[stArr.Length - 1];
}
return st;
}
public static void Generate(bool Desktop, bool RemoveLast, bool InExistingTxt, int Count, int Length)
{
Random Generator = new Random();
if (Desktop)
path = $"C:\Users\{GetWindowsUserName()}\Desktop\GeneratedNumbers.txt";
else
path = "GeneratedNumbers.txt";
if (!InExistingTxt && !RemoveLast)
File.Create(path);
else if (!InExistingTxt && RemoveLast)
{
if (File.Exists(path))
{
File.Delete(path);
}
File.Create(path);
}
System.Threading.Thread.Sleep(1000);
if (File.Exists(path))
{
StreamWriter SW = new StreamWriter(path);
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
SW.Dispose();
}
}
File.Create
returns 创建文件的流。由于您没有处理流,因此在尝试重新打开同一文件时出错。
我还怀疑你搞砸了你的 "RemoveLast" 逻辑。我假设您想在现有文件设置为 false 时将内容附加到现有文件:
if (InExistingTxt && !File.Exists(path))
return;
StreamWriter SW;
if (RemoveLast)
SW = File.CreateText(path);
else
SW = File.AppendText(path);
using (SW)
{
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
}
当我调用 Generate
函数时,它不会创建 StreamWriter
对象,而是抛出一个异常:
file used by another process
但是文件没有打开,这是第一个使用它的流。
public static string GetWindowsUserName()
{
string st = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
bool Condition = false;
foreach (char ch in st)
{
if (ch == '\')
Condition = true;
}
if (Condition)
{
string[] stArr = st.Split('\');
st = stArr[stArr.Length - 1];
}
return st;
}
public static void Generate(bool Desktop, bool RemoveLast, bool InExistingTxt, int Count, int Length)
{
Random Generator = new Random();
if (Desktop)
path = $"C:\Users\{GetWindowsUserName()}\Desktop\GeneratedNumbers.txt";
else
path = "GeneratedNumbers.txt";
if (!InExistingTxt && !RemoveLast)
File.Create(path);
else if (!InExistingTxt && RemoveLast)
{
if (File.Exists(path))
{
File.Delete(path);
}
File.Create(path);
}
System.Threading.Thread.Sleep(1000);
if (File.Exists(path))
{
StreamWriter SW = new StreamWriter(path);
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
SW.Dispose();
}
}
File.Create
returns 创建文件的流。由于您没有处理流,因此在尝试重新打开同一文件时出错。
我还怀疑你搞砸了你的 "RemoveLast" 逻辑。我假设您想在现有文件设置为 false 时将内容附加到现有文件:
if (InExistingTxt && !File.Exists(path))
return;
StreamWriter SW;
if (RemoveLast)
SW = File.CreateText(path);
else
SW = File.AppendText(path);
using (SW)
{
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
}