文件操作问题,The process cannot access the file "E:\1.jpg" because it is being used by anther process

wyy1001 2007-01-28 12:45:46
我别的地方没有使用到FileStream f=new FileStream(....)

为什么我现在执行到FileStream f=new FileStream(....)
出现了问题呢?

...全文
2736 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuchao330388028 2010-03-16
  • 打赏
  • 举报
回复
要先关闭才可以
yuchao330388028 2010-03-16
  • 打赏
  • 举报
回复
我也遇到这样的问题了啊
zzx509 2010-03-16
  • 打赏
  • 举报
回复
Bitmap bm = new Bitmap(fileListItem);
这一句从文件创建位图实际已经独占了了文件。
可以改成这样。
using (Bitmap bit = new Bitmap(fileListItem))
{

this.panel1.BackgroundImage = bit.GetThumbnailImage(panel1.Width, panel1.Height, null, IntPtr.Zero);
}


另外你的文件上传代码中
FileStream fs = File.Create(filename[x].ToString());
//这里出现了问题
byte[] info=new byte[fs.Length];
fs.Close();
这几句似乎逻辑就有问题。文件已经存在了,为什么又要新建文件而不是打开读取,那文件长度字节不是成为0了吗?
犀山居士 2007-09-13
  • 打赏
  • 举报
回复
请问 new FileIOPermission(PermissionState.Unrestricted).Assert();这行是什么作用?而这里未指明要访问哪个路径啊?
我写了个类,将文件上传到服务器上,在本机上可以正常,但服务器上则不行,提示错误:
Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

网上找了关于安全的资料,没找到相关的说法,有没有高手这方面的资料?
ke5315309 2007-01-28
  • 打赏
  • 举报
回复
打代码拿出来看看
心晴Sunheart 2007-01-28
  • 打赏
  • 举报
回复
应该是其他的应用程序已经打开了你要读的文件
smartstar2005 2007-01-28
  • 打赏
  • 举报
回复
最有可能的原因是你程序中在访问这个文件之后没有关掉。

这样你第一次运行的时候可能没有问题。但由于没有释放访问权限,第二次的时候就不能再访问了。我曾经遇到过这样的问题。
wyy1001 2007-01-28
  • 打赏
  • 举报
回复
1.drag图片时:
private void panel1_DragDrop(object sender, DragEventArgs e)
{
try
{
new FileIOPermission(PermissionState.Unrestricted).Assert();
// StringBuilder fileTable = new StringBuilder();
foreach (string fileListItem in (e.Data.GetData(DataFormats.FileDrop) as string[]))
{
Bitmap bm = new Bitmap(fileListItem);
//get image name
int pos = fileListItem.LastIndexOf(@"\") + 1;
string name = fileListItem.Substring(pos); ;
str = str + 1;
Drag darg = new Drag(bm, name, str-2, ds);
darg.Left = x * 175 + 15 * (x + 1) + panel1.AutoScrollPosition.X;
darg.Top = y * 150 + 10 * (y + 1) + panel1.AutoScrollPosition.Y;
darg.Width = 175;
darg.Height = 150;
//imgList[x] = fileListItem;

dr = ds.Tables[0].NewRow();
dr[0] = x;
dr[1] = fileListItem;
ds.Tables[0].Rows.Add(dr);

this.panel1.Controls.Add(darg);

x += 1;
if (x % 3 == 0)
{
x = 0;
y += 1;

}

}


CodeAccessPermission.RevertAssert();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

2。上传图片时
private void CopyWithProgress(string[] filenames)
{


// Display the ProgressBar control.
this.progressBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
progressBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
progressBar1.Maximum = filenames.Length;
// MessageBox.Show(filenames.Length.ToString());
// Set the initial value of the ProgressBar.
progressBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
progressBar1.Step = 1;

for (int x = 0; x < filenames.Length; x++)
{

int pos = filenames[x].LastIndexOf(@"\") + 1;

string filename = filenames[x].Substring(pos);
this.labFilename.Text = filenames[x];
//这里出现了问题,
FileStream fs = File.Create(filename[x].ToString());
//这里出现了问题
byte[] info=new byte[fs.Length];
fs.Close();
if (uploadService.UploadFiles(info, @"img\" + filename))
{
// MessageBox.Show("Upload Images");
}
else
{
MessageBox.Show("No Upload Images");

}




progressBar1.PerformStep();
Bitmap bm = new Bitmap(filenames[x]);
this.pictureBox1.Image = bm;
}



}
burn3tt 2007-01-28
  • 打赏
  • 举报
回复
你是不是在其他地方打开了这个文件没有关闭?检查检查代码,或者打开任务管理器看看哪些可疑进程
xiaomatian 2007-01-28
  • 打赏
  • 举报
回复
文件正在被使用。把代码拿出来看看。
dr_lou 2007-01-28
  • 打赏
  • 举报
回复
FileStream fs = new (3个参数)

我估计是后两个参数的问题
blessyou312 2007-01-28
  • 打赏
  • 举报
回复
有代码才好分析嘛

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧