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

jimcharles 2003-10-20 11:44:40
我原本想在向本地写入文件一样用“FileStream”来实现,可是老是报错,好像是权限不够,但是我已经在系统中用administrator登陆了这台网路中的机器,是哪里出了问题??怎么解决?要在程序中设置登陆的用户和密码么??
...全文
117 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
  • 打赏
  • 举报
回复
没人知道??

110,533

社区成员

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

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

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