本文介绍了我在哪里可以在代码中提及fileio.write权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴了我的邮政编码文件代码..
谁能告诉我有任何错误.
我可以在这里给任何file.io写入权限.

Crc32 crc =新的Crc32();
字符串附加= AppDomain.CurrentDomain.BaseDirectory +文档/" +"Attachments.zip";
//FileStream fs1 = new FileStream(attach,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite,System.IO.FileShare.Write);



如果(File.Exists(attach))
{
File.Delete(attach);
}
ZipOutputStream s = new ZipOutputStream(File.Create(attach));
s.SetLevel(9); //0-仅存储到9-意味着最佳压缩


//ZipEntry条目= null;
字符串名称= hidfield.Value;
string [] strNames = names.Split('','');

//byte [] buffer = null;
for(int i = 0; i< strNames.Length; i ++)
{

FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory +"Documents/" + strNames [i]);

byte []缓冲区=新的byte [fs.Length];
fs.Read(buffer,0,buffer.Length);

ZipEntry条目=新的ZipEntry(ZipEntry.CleanName("Documents/" + strNames [i]));
entry.DateTime = DateTime.Now;
entry.Comment =";
entry.ZipFileIndex = i;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer,0,buffer.Length);
}
s.Finish();
s.Close();


我遇到了类似
的错误
该进程无法访问文件"path \ Attachments.zip",因为它正在被另一个进程使用.

i posted my zip a files code..
cananyone tell me is there any error.
here can i give any file.io write permissions.

Crc32 crc = new Crc32();
string attach = AppDomain.CurrentDomain.BaseDirectory + "Documents/" + "Attachments.zip";
// FileStream fs1=new FileStream(attach,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite, System.IO.FileShare.Write);



if (File.Exists(attach))
{
File.Delete(attach);
}
ZipOutputStream s = new ZipOutputStream(File.Create(attach));
s.SetLevel(9); // 0 - store only to 9 - means best compression


//ZipEntry entry = null;
string names = hidfield.Value;
string[] strNames = names.Split('','');

//byte[] buffer = null;
for (int i = 0; i < strNames.Length; i++)
{

FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "Documents/" + strNames[i]);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

ZipEntry entry = new ZipEntry(ZipEntry.CleanName("Documents/" + strNames[i]));
entry.DateTime = DateTime.Now;
entry.Comment = " ";
entry.ZipFileIndex = i;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();


i got error like

The process cannot access the file ''path\Attachments.zip'' because it is being used by another process

推荐答案

写道:​​

字符串附加= AppDomain.CurrentDomain.BaseDirectory +文档/" +"Attachments.zip";

string attach = AppDomain.CurrentDomain.BaseDirectory + "Documents/" + "Attachments.zip";



使用System.IO.Path.Combine.这是越野车,但比这要好.

您的错误与权限无关.这就是说的话.您还有一些其他代码已打开此文件.您需要调试代码,以便一次只能尝试打开一个文件,并且所有尝试打开该文件的操作都以调用Close()将其关闭为结束.



Use System.IO.Path.Combine. It''s buggy, but it''s better than this.

Your error has NOTHING to do with permissions. It means what it says. You have some other code that has this file open already. You need to debug your code so only one attempt at a time is made to open a file and that all attempts to open it, end with calling Close() to close it.


这篇关于我在哪里可以在代码中提及fileio.write权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:51