该进程无法访问文件,因为该文件正由另一进程使用!
2007-09-21 11:38
267 查看
用asp.net文件读写操作时错误: 该进程无法访问文件,因为该文件正由另一进程使用!
在此引用MSDN中的示例解决:
1 using System;
2 using System.IO;
3
4 class Test
5 {
6 public static void Main()
7 {
8 string path = @"c:\MyTest.txt";
9 // This text is added only once to the file.
10 if (!File.Exists(path))
11 {
12 // Create a file to write to.
13 using (StreamWriter sw = File.CreateText(path)) //在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。
14 {
15 sw.WriteLine("Hello");
16 sw.WriteLine("And");
17 sw.WriteLine("Welcome");
18 }
19 }
20
21 // This text is always added, making the file longer over time
22 // if it is not deleted.
23 using (StreamWriter sw = File.AppendText(path)) {
24 sw.WriteLine("This");
25 sw.WriteLine("is Extra");
26 sw.WriteLine("Text");
27 }
28
29 // Open the file to read from.
30 using (StreamReader sr = File.OpenText(path))
31 {
32 string s = "";
33 while ((s = sr.ReadLine()) != null)
34 {
35 Console.WriteLine(s);
36 }
37 }
38 }
39 }
注意上面的using语句一定要用,否则就会出现上面的错误,希望对各位有帮助!----我是菜鸟
在此引用MSDN中的示例解决:
1 using System;
2 using System.IO;
3
4 class Test
5 {
6 public static void Main()
7 {
8 string path = @"c:\MyTest.txt";
9 // This text is added only once to the file.
10 if (!File.Exists(path))
11 {
12 // Create a file to write to.
13 using (StreamWriter sw = File.CreateText(path)) //在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。
14 {
15 sw.WriteLine("Hello");
16 sw.WriteLine("And");
17 sw.WriteLine("Welcome");
18 }
19 }
20
21 // This text is always added, making the file longer over time
22 // if it is not deleted.
23 using (StreamWriter sw = File.AppendText(path)) {
24 sw.WriteLine("This");
25 sw.WriteLine("is Extra");
26 sw.WriteLine("Text");
27 }
28
29 // Open the file to read from.
30 using (StreamReader sr = File.OpenText(path))
31 {
32 string s = "";
33 while ((s = sr.ReadLine()) != null)
34 {
35 Console.WriteLine(s);
36 }
37 }
38 }
39 }
注意上面的using语句一定要用,否则就会出现上面的错误,希望对各位有帮助!----我是菜鸟
相关文章推荐
- 无法将临时文件复制到输出目录和无法将文件 assembly name 复制到运行目录。 进程无法访问该文件,因为它正由另一个进程使用
- 包含别人的dll,然后我们用类库再次封装成dll的时候的注意事项;源文件与模块生成时的文件不同;创建调试信息文件 ··PDB时发生意外的错误,进程无法访问文件,因为另一个程序正在使用此文件
- C#关于"该进程无法访问文件,因为该文件正由另一进程使用"的错误的一点心得(示例)
- C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件
- IIS网站启动不了,错误提示“另一个程序正在使用此文件,进程无法访问”
- IIS中另一个程序正在使用此文件,进程无法访问
- Lock不住的BUG,神一般无解的BUG(XX正由另一进程使用,因此该进程无法访问该文件)
- Cannot create file"C:\Users\LML\AppData\Local\Temp\EditorLineEnds.ttr"。另一个程序正在使用此文件,进程无法访问。
- 正由另一进程使用,因此该进程无法访问该文件
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- IIS7出现“另一个程序正在使用此文件,进程无法访问”
- 多线程写文件异常(正由另一进程使用,因此该进程无法访问该文件)的解决方法
- SharePoint 2013 另一个程序正在使用此文件,进程无法访问。 (异常来自 HRESULT:0x80070020)
- 无法将文件“.exe”复制到“.exe”。文件“.exe”正由另一进程使用,因此该进程无法访问该文件
- Windows Azure Storage Emulator: 另一程序正在使用此文件进程无法访问
- VS保存和编译问题] 总是出现另一个程序正在使用此文件,进程无法访问
- IIS 中 "另一个程序正在使用此文件,进程无法访问!" http://www.cnblogs.com/publicbill/archive/2007/05/29/763844.html
- 文件“D:\file.txt”正由另一进程使用,因此该进程无法访问该文件。
- 启动IIS 网站出现错误 另一个程序正在使用此文件,进程无法访问
- 对于image.fromfile方法在使用时,遇到的“正由另一进程使用,因此该进程无法访问此文件”解决方法