如何在C#应用程序或asp.net的web应用中,向一个映射到本地的网络盘中写入文件?

jimcharles 2003-10-20 11:44:40
我原本想在向本地写入文件一样用“FileStream”来实现,可是老是报错,好像是权限不够,但是我已经在系统中用administrator登陆了这台网路中的机器,是哪里出了问题??怎么解决?要在程序中设置登陆的用户和密码么??
...全文
121 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
msm 2003-12-01
  • 打赏
  • 举报
回复
侯勇峰的方法最简单
wolve 2003-10-22
  • 打赏
  • 举报
回复
你可能要使用Permission。
// This class represents the application itself
class App {
public static void Main() {

// Try to access resources using the permissions currently available.
AttemptAccess("Default permissions");



// Create a permission set that allows read access to the TEMP
// environment variable and read, write, and append access to SomeFile
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP"));
ps.AddPermission(
new FileIOPermission(FileIOPermissionAccess.Read |
FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,
Path.GetFullPath("SomeFile")));


// Stop security checks at this point in the stack walk
// for the specified permissions
ps.Assert();

// Try to access resources using the permissions we've just asserted.
AttemptAccess("Assert permissions");

// Remove this stack frame's Assert
CodeAccessPermission.RevertAssert();


// Deny access to the resources we specify
ps.Deny();

// Try to access resources using the permissions we've just denied.
AttemptAccess("Deny permissions");

// Remove this stack frame's Deny so we're back to default permissions.
CodeAccessPermission.RevertDeny();



// Make the permissions indicate the only things that we're allowed to do.
ps.PermitOnly();

// Try to access resources using only the permissions we've just permitted.
AttemptAccess("PermitOnly permissions");

// Remove this stack frame's PermitOnly so we're back to default permissions.
CodeAccessPermission.RevertPermitOnly();



// Remove the FileIOPermissions from the permission set
ps.RemovePermission(typeof(FileIOPermission));


// Try to access resources using only the Environment permissions.
ps.PermitOnly();
AttemptAccess("PermitOnly without FileIOPermission permissions");
CodeAccessPermission.RevertPermitOnly();

// Remove the EnvironmentPermission from the permission set
ps.RemovePermission(typeof(EnvironmentPermission));


// Try to access resources using no permissions.
ps.PermitOnly();
AttemptAccess("PermitOnly without any permissions");
CodeAccessPermission.RevertPermitOnly();


// Show how to use Demand/Assert to improve performance
CopyFile(".\\Permissions.exe", ".\\Permissions.copy.exe");

// Delete .exe copy
File.Delete(".\\Permissions.copy.exe");
}


static public void AttemptAccess(String s) {
FileStream fs = null;
String ev = null;

// Try to access a file
try {
fs = new FileStream("SomeFile", FileMode.OpenOrCreate);
}
catch (Exception) {
}

// Try to read an environment variable
try {
ev = Environment.GetEnvironmentVariable("TEMP");
}
catch (Exception) {
}

// Display what we sucessfully did.
Console.WriteLine(s + " test: "
+ ((fs != null) ? "FileOpen" : "FileNOTOpened") + ", "
+ ((ev != null) ? "EVRead" : "EVNOTRead"));

// If we opened the file, close it & delete it
if (fs != null) {
fs.Close();
File.Delete("SomeFile");
}
}


public static void CopyFile(String srcPath, String dstPath) {

// Create a file permission set indicating all of this method's intentions.
FileIOPermission fp = new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath(srcPath));
fp.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Append, Path.GetFullPath(dstPath));

// Verify that we can be granted all the permissions we'll need.
fp.Demand();

// Assert the desired permissions here.
fp.Assert();

// For the remainder of this method, demands for source file read access
// and demands for destination file write/append access will be granted
// immediately; walking the remainder of the stack will not be necessary.

FileInfo srcFile = new FileInfo(srcPath);
FileInfo dstFile = new FileInfo(dstPath);
Stream src = srcFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
Stream dst = dstFile.Open(FileMode.Create, FileAccess.Write, FileShare.None);
if (srcFile.Length > Int32.MaxValue)
throw new Exception("CopyFile requires that the source file be less than 2GB.");
Byte[] buffer = new Byte[(Int32) srcFile.Length];
src.Read(buffer, 0, (Int32) srcFile.Length);
dst.Write(buffer, 0, (Int32) srcFile.Length);

src.Close();
dst.Close();

// We do not need a RevertAssert here because we are going out of scope
}
}

这是.net的示例,有关Permission,参考:ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemsecuritypermissionsetclasstopic.htm
rockrabbit 2003-10-22
  • 打赏
  • 举报
回复
good。
jimcharles 2003-10-22
  • 打赏
  • 举报
回复
主要代码如下,第一行就报错了,r盘为已经映射的网络盘
FileStream fs = new FileStream("r:\\send.txt",FileMode.Create,FileAccess.Write);
//建立StreamWriter为写做准备
StreamWriter rw = new StreamWriter(fs,Encoding.Default);
//使用WriteLine写入内容
rw.WriteLine("1");
//将缓冲区的内容写入文件
rw.Flush();
rw.Close();
fs.Close();
jimcharles 2003-10-22
  • 打赏
  • 举报
回复
我在系统中对这个网络盘能正常读写(此网络盘的可能源自Novell的服务器或Windows系统的电脑),就是程序中无法写入文件
报错为“System.IO.IOException: 当前没有可用的登录服务器来服务登录请求。”
程序中该如何指定登陆名和密码呢??
xz_king 2003-10-21
  • 打赏
  • 举报
回复
网路中的机器的文件夹也要设置成任何人可以读写,
lilycarol 2003-10-21
  • 打赏
  • 举报
回复
.net framework 对WEB应用程序有权限控制,可以修改
控制面板->管理工具->本地安全策略 修改对本地的访问权限
Montaque 2003-10-21
  • 打赏
  • 举报
回复
shell("cmd.exe /c Net use \\pc1 password /user:userName")
ajex 2003-10-21
  • 打赏
  • 举报
回复
你必须保证aspnet对于远程机器共享的目录有write权限.
shajie 2003-10-21
  • 打赏
  • 举报
回复
这应该是属于文件上传的问题吧。
jimcharles 2003-10-21
  • 打赏
  • 举报
回复
没人知道??
c#图形书最经典的一本书当包括饼图,条形图,绘图板制作等 第1章 GDI+ ——下一代图形接口 1.1 理解GDI+ 1.2 探索GDI+ 的功能 1.3 从GDI的角度学习GDI+ 1.4 .NET的GDI+ 名称空间和类 总结 第2章 第一个GDI+ 应用程序 2.1 绘制表面 2.2 坐标系统 2.3 指南——第一个GDI+ 应用程序 2.4 一些基本的GDI+ 对象 总结 第3章 Graphics类 3.1 Graphics类的属性 3.2 Graphics类的方法 3.3 GDI+ Painter应用程序 3.4 绘制饼图 总结 第4章 使用画笔和钢笔 4.1 理解和使用画笔 4.2 在GDI+ 使用钢笔 4.3 使用钢笔进行变形 4.4 使用画笔进行变形 4.5 系统钢笔和系统画笔 4.6 一个真实世界的例子 ——在GDI+ Painter应用程序添加颜色、钢笔和画笔 总结 第5章 颜色、字体和文本 5.1 访问Graphics对象 5.2 使用颜色 5.3 使用字体 5.4 使用文本和字符串 5.5 渲染文本的质量和性能 5.6 高级版式 5.7 一个简单的文本编辑器 5.8 文本变形 总结 第6章 矩形和区域 6.1 Rectangle结构体 6.2 Region类 6.3 区域和剪辑 6.4 剪辑区域示例 6.5 区域、非矩形窗体和控件 总结 第7章 图像处理 7.1 光栅图像和矢量图像 7.2 使用图像 7.3 操作图像 7.4 在GDI+ 播放动画 7.5 使用位图 7.6 使用图标 7.7 扭曲图像 7.8 绘制透明的图形对象 7.9 查看多个图像 7.10 使用图片框查看图像 7.11 使用不同的大小保存图像 总结 第8章 高级图像处理 8.1 渲染位图的一部分 8.2 使用图元文件 8.3 使用颜色对象应用颜色映射 8.4 图像属性和ImageAttributes类 8.5 编码器参数与图像格式 总结 第9章 高级二维图形 9.1 线帽和线条样式 9.2 理解并使用图形路径 9.3 图形容器 9.4 读取图像的元数据 9.5 混合 9.6 Alpha混合 9.7 其他高级二维主题 总结 第10章 变形 10.1 坐标系统 10.2 变形的类型 10.3 Matrix类与变形 10.4 Graphics类与变形 10.5 全局变形、局部变形和复合变形 10.6 图像变形 10.7 颜色变形和颜色矩阵 10.8 图像处理的矩阵操作 10.9 文本变形 10.10 变形顺序的重要性 总结 第11章 打印 11.1 简要地回顾使用Microsoft Windows进行打印的历史 11.2 打印过程概述 11.3 第一个打印应用程序 11.4 打印机的设置 11.5 PrintDocument和Print事件 11.6 打印文本 11.7 打印图形 11.8 打印对话框 11.9 自定义页面设置 11.10 打印多个页面 11.11 页边打印——注意事项 11.12 进入细节——自定义控制和打印控制器 总结 第12章 开发GDI+ Web应用程序 12.1 创建第一个ASP.NET Web应用程序 12.2 第一个图形Web应用程序 12.3 绘制简单的图形 12.4 在Web上绘制图像 12.5 绘制曲线图 12.6 绘制饼图 总结 第13章 GDI+ 的最佳实践及性能技术 13.1 理解渲染过程 13.2 双缓存和无抖动绘图 13.3 理解SetStyle方法 13.4 绘图过程的质量与性能 总结 第14章 GDI互操作性 14.1 在受控环境使用GDI 14.2 在受控代码使用GDI的注意事项 总结 第15章 其他GDI+ 示例 15.1 设计交互式GUI应用程序 15.2 绘制具有形状的窗体和Windows控件 15.3 为绘制的图像添加版权信息 15.4 从流或数据库读取及写入图像 15.5 创建自绘制的列表控件 总结 附录A .NET的异常处理

110,536

社区成员

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

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

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