C# C“System.IO IOException”;无法访问文件,因为它被另一个进程访问;
我刚开始使用System.IO,我不知道为什么我的代码会导致这种异常。 我想检查目录和文件是否存在,如果不存在,我想创建它们。在那之后,我想在我刚刚创建的文件上写一些东西。这里它抛出了异常。我非常确定,当我尝试使用StreamWriter时,创建会导致异常,因为如果文件已经存在,我无法获得执行选项。此外,当我在一次失败的尝试后再次单击调用此函数的按钮时,没有异常,一切正常(看起来我的程序在刷新UI后意识到没有打开的进程)。我不明白,还有什么进程似乎在访问文件,我想在使用FileInfo.Create()函数之后,我不需要关闭任何流 这是我的密码:C# C“System.IO IOException”;无法访问文件,因为它被另一个进程访问;,c#,.net,C#,.net,我刚开始使用System.IO,我不知道为什么我的代码会导致这种异常。 我想检查目录和文件是否存在,如果不存在,我想创建它们。在那之后,我想在我刚刚创建的文件上写一些东西。这里它抛出了异常。我非常确定,当我尝试使用StreamWriter时,创建会导致异常,因为如果文件已经存在,我无法获得执行选项。此外,当我在一次失败的尝试后再次单击调用此函数的按钮时,没有异常,一切正常(看起来我的程序在刷新UI后意识到没有打开的进程)。我不明白,还有什么进程似乎在访问文件,我想在使用FileInfo.Crea
public static void Save_map(int[,] p_temp_map_property, string p_filename)
{
temp_map_property = p_temp_map_property;
try
{
DirectoryInfo MapsDirectory = new DirectoryInfo("Maps");
DirectoryInfo Map = new DirectoryInfo(@"Maps\" + p_filename);
FileInfo file = new FileInfo(@"Maps\" + p_filename + @"\" + p_filename + ".txt");
if (!MapsDirectory.Exists)
{
MapsDirectory.Create();
}
if (!Map.Exists)
{
Map.Create();
}
if (!file.Exists)
{
file.Create();
}
StreamWriter sw = new StreamWriter(@"Maps\" + p_filename + @"\" + p_filename + ".txt", false);
for(int n = 0; n < 140; n++)
{
for(int m = 0; m < 90; m++)
{
sw.WriteLine(p_temp_map_property[n, m]);
}
}
sw.Close();
sw.Dispose();
MessageBox.Show("Map saved successfully!");
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
我希望比我更有经验的人能帮我。
提前感谢。file.Create()代码>使文件保持打开状态,并返回句柄。要么先关闭它,要么用它来写东西
下面的代码使用它:
public static void Save_map(int[,] p_temp_map_property, string p_filename)
{
temp_map_property = p_temp_map_property;
try
{
DirectoryInfo MapsDirectory = new DirectoryInfo("Maps");
DirectoryInfo Map = new DirectoryInfo(@"Maps\" + p_filename);
FileInfo file = new FileInfo(@"Maps\" + p_filename + @"\" + p_filename + ".txt");
if (!MapsDirectory.Exists)
{
MapsDirectory.Create();
}
if (!Map.Exists)
{
Map.Create();
}
using (FileStream fileStream = file.Exists ? file.OpenWrite() : file.Create())
using (StreamWriter sw = new StreamWriter(fileStream, false))
{
for (int n = 0; n < 140; n++)
{
for (int m = 0; m < 90; m++)
{
sw.WriteLine(p_temp_map_property[n, m]);
}
}
}
MessageBox.Show("Map saved successfully!");
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
public static void Save_map(int[,]p_temp_map_属性,字符串p_filename)
{
临时映射属性=临时映射属性;
尝试
{
DirectoryInfo MapsDirectory=新的DirectoryInfo(“地图”);
DirectoryInfo映射=新的DirectoryInfo(@“映射\”+p_文件名);
FileInfo file=newfileinfo(@“Maps\”+p\u filename+@“\”+p\u filename+.txt”);
如果(!MapsDirectory.Exists)
{
MapsDirectory.Create();
}
如果(!Map.Exists)
{
Map.Create();
}
使用(FileStream FileStream=file.Exists?file.OpenWrite():file.Create())
使用(StreamWriter sw=newstreamwriter(fileStream,false))
{
对于(int n=0;n<140;n++)
{
对于(int m=0;m<90;m++)
{
软件写入线(p_temp_map_属性[n,m]);
}
}
}
MessageBox.Show(“地图保存成功!”);
}
捕获(例外e)
{
Show(例如ToString());
}
}
尝试实现“using”语句,因为它在以下说明之后关闭对象:
string path = @"c:\MyTest.txt";
FileInfo fi = new FileInfo(path);
using (FileStream fs = fi.Create())
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
//Add some information to the file.
fs.Write(info, 0, info.Length);
}
您将打开该文件两次1)FileInfo file=newfileinfo(@“Maps\”+p\u filename+@“\”+p\u filename+“.txt”);2) StreamWriter sw=新StreamWriter(@“映射\”+p\U文件名+@“\”+p\U文件名+.txt),false);部分正确。初始化FileInfo
类不会打开文件流,但调用.Create()
会打开文件流。您不需要对目录存在性进行所有这些测试。只需调用DirectoryInfo.Create,其中包含您要创建的最后一个目录的完整路径。如果目录不存在,该方法将为您创建目录以供将来参考:所有错误消息必须逐字复制/粘贴到您的问题中。因此,在发布涉及任何错误消息的问题之前,您应该在网站上搜索错误消息的确切文本,并查看可用信息是否解决了您的问题。您在此处遇到的错误非常常见,并且已有大量信息可用于帮助您解决问题,而无需发布新问题。sw.Dispose
在使用
块中是毫无意义的,.Close
可能也是如此,如果使用
块在中有sw
。在fileStream
上只有一个使用块。我只是尽可能地重复使用OPs代码。尽管如此,我还是同意尽我所能改进它更好:)更新。非常感谢你的帮助。它帮助我理解了这个问题
string path = @"c:\MyTest.txt";
FileInfo fi = new FileInfo(path);
using (FileStream fs = fi.Create())
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
//Add some information to the file.
fs.Write(info, 0, info.Length);
}