Wednesday, 17 June 2015
How to fix error The process cannot access the file because it is being used by another process.
Watch this example on YouTube:
An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code
Additional information: The process cannot access the file because it is being used by another process.
REPLACE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace WebApplication1
{
public partial class OpenFileIssue : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string FileDirectory = "C:\\Temp\\ExcelTest\\Test.csv";
var reader = new StreamReader(File.OpenRead(FileDirectory));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values[1].Trim() != String.Empty)
{
Response.Write("found something<br />");
}
}
reader = new StreamReader(File.OpenRead(FileDirectory));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values[1].Trim() != String.Empty)
{
Response.Write("found something");
}
}
reader.Close();
string FileDirMoved = "C:\\Temp\\ExcelTest\\Moved\\Test.csv";
File.Move(FileDirectory, FileDirMoved);
}
}
}
WITH:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace WebApplication1
{
public partial class OpenFileIssue : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string FileDirectory = "C:\\Temp\\ExcelTest\\Test.csv";
var reader = new StreamReader(File.OpenRead(FileDirectory));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values[1].Trim() != String.Empty)
{
Response.Write("found something<br />");
}
}
reader.Close();
reader = new StreamReader(File.OpenRead(FileDirectory));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values[1].Trim() != String.Empty)
{
Response.Write("found something");
}
}
reader.Close();
string FileDirMoved = "C:\\Temp\\ExcelTest\\Moved\\Test.csv";
File.Move(FileDirectory, FileDirMoved);
}
}
}
Subscribe to:
Post Comments (Atom)
thanks it helped
ReplyDelete