access的用法及同义词
作者:百变鹏仔日期:2023-07-19 22:09:52浏览:12分类:文字大全
access(判断是否具有存取文件的权限)
相关函数 stat,open,chmod,chown,setuid,setgid
表头文件 #include
定义函数 int access(const char * pathname,int mode);
函数说明 access()会检查是否可以读/写某一已存在的文件。参数mode有几种情况组合,R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK与X_OK用来检查文件是否具有读取、写入和执行的权限。F_OK则是用来判断该文件是否存在。由于access()只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve()执行时则会失败。
返回值 若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1。
错误代码 EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。
EROFS 欲测试写入权限的文件存在于只读文件系统内。
EFAULT 参数pathname指针超出可存取内存空间。
EINVAL 参数mode 不正确。
ENAMETOOLONG 参数pathname太长。
ENOTDIR 参数pathname为一目录。
ENOMEM 核心内存不足
ELOOP 参数pathname有过多符号连接问题。
EIO I/O 存取错误。
附加说明 使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。
范例 /* 判断是否允许读取/etc/passwd */
#include
int main()
{
if (access(“/etc/passwd”,R_OK) = =0)
printf(“/etc/passwd can be read\n”);
}
执行 /etc/passwd can be read
(一)把文件内容写入Access数据库的OLE对象字段中:
if (File.Exists(txtBrow.Text) != false) // 文本框txtBrow中内容为文件路径及文件名
{
//获取文件后缀
FileInfo p = new FileInfo(txtBrow.Text.Trim());
F_str_Type = p.Extension.ToLower();
if (F_str_Type.Length > 5)
{
MessageBox.Show("不可识别的文件格式,请重新确认!","警告");
return;
}
//判断文件大小
if (p.Length == 0)
{
MessageBox.Show("文件的大小为“0”,不能保存!", "警告");
return;
}
//创建文件对象以打开的形式读取文件
FileStream sFileStream = new FileStream(txtBrow.Text, FileMode.Open);
//分配数组大小
byte[] bFile = new byte[sFileStream.Length];
//将文件内容读进数组
sFileStream.Read(bFile, 0, (int)sFileStream.Length);
//关闭文件对象
sFileStream.Close();
//查找文档类别号
OleDbDataReader topicread = SaveConn.GetReader("select File_ID from FileTopic where File_Topic='" + cbbTopic.Text.Trim() + "'");
//Read()方法用来读取OleDbDataReader对象中的记录
topicread.Read();
T_int_Topic=(int)topicread["File_ID"];
OleDbConnection conn = SaveConn.GetConn();
conn.Open();
OleDbCommand com = conn.CreateCommand();
com.CommandText = "insert into FileTitle(File_CodeID,File_TopicID,File_Title,File_Time,File_Save,File_Name,File_Type) values(@File_CodeID,@File_TopicID,@File_Title,@File_Time,@File_Save,@File_Name,@File_Type)";
com.Parameters.AddWithValue("@File_CodeID", txtCode.Text.Trim()); //文档编号
com.Parameters.AddWithValue("@File_TopicID", T_int_Topic); //文档类别号
com.Parameters.AddWithValue("@File_Title", txtTitle.Text.Trim()); //文档标题
com.Parameters.AddWithValue("@File_Time", txtTime.Text.Trim()); //存储时间
com.Parameters.AddWithValue("@File_Save", bFile); //文档内容
com.Parameters.AddWithValue("@File_Name", txtName.Text.Trim()); //经手人
com.Parameters.AddWithValue("@File_Type", F_str_Type); //文档类型
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("文档资料添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
(二)双击DataGridView控件某一行,显示文档内容
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
string F_str_Open;
//如果不存在就创建Temp文件夹
if (Directory.Exists(Application.StartupPath + @"/Temp") == false)
{
Directory.CreateDirectory(Application.StartupPath + @"/Temp");
}
try
{
OleDbDataReader sqlread = SaveConn.GetReader("select File_Save,File_Type from FileTitle where File_CodeID='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
sqlread.Read();
//判断文件是否已存在.
if (File.Exists(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]))
{
//如存在则删除
File.Delete(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]);
}
byte[] bFile=(byte[])sqlread[0];
//创建文件对象
System.IO.FileStream sFileStream = new System.IO.FileStream(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"], System.IO.FileMode.Create);
//将数组的内容写进文件
sFileStream.Write(bFile, 0, bFile.Length);
//关闭文件
sFileStream.Close();
Process p = new Process();
p.StartInfo.FileName = Convert.ToString(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]);
p.Start();
p.Close();
sqlread.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
鹏仔 微信 15129739599百科狗 baikegou.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
- 上一篇:已经是第一篇了
- 下一篇:已经是最后一篇了
- 搜索