当处理有关文件流的web服务的时候,调试时出现异常:
System.UnauthorizedAccessException: Access to the path 'some file path' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
没有文件的访问权限。解决办法有二:
1.在web.config文件的<system.web>节点里添加一行:
这个userName和password就是asp.net做访问的身份,在IIS里可以看到。
2.在web.config文件的<system.web>节点里添加一行:
然后对你要操作的文件目录增加用户,也就是ASP.net的用户,一般都是“IUSER_computerName”,不清楚的话,可以从IIS里copy出来。
第一个的优点在于一劳永逸,以后文件目录变了也没事;后者的长处在于直接增加用户就可以了,尤其在密码不清楚的情况下。
一个文件流操作的web服务的例子:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
///
/// Summary description for WebService
///
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public struct WebFile
{
public string Name;
public byte[] Contents;
public DateTime CreationTime;
public DateTime LastAccessTime;
public DateTime LastWriteTime;
}
///
/// Summary description for Service1.
///
public class WebDirectory : System.Web.Services.WebService
{
public WebDirectory()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
[WebMethod]
public string[] ListFiles()
{
return Directory.GetFiles("c:\Public");
}
[WebMethod]
public WebFile GetFile(string fileName)
{
WebFile webFile = new WebFile();
string filePath = "c:\Public\" + fileName;
// Set the name of the file.
webFile.Name = fileName;
// Obtain the contents of the requested file.
Stream s = File.Open(filePath, FileMode.Open);
webFile.Contents = new byte[s.Length];
s.Read(webFile.Contents, 0, (int)s.Length);
// Retrieve the date/time stamps for the file.
webFile.CreationTime = File.GetCreationTime(filePath);
webFile.LastAccessTime = File.GetLastAccessTime(filePath);
webFile.LastWriteTime = File.GetLastWriteTime(filePath);
return webFile;
}
}
}
客户端,控制台应用程序
using System;
using System.IO;
using WebFileUtil.localhost;
namespace WebFileUtil
{
public class WebFileUtil
{
static int Main(string[] args)
{
// Validate number of command line arguments.
if (!((args.Length == 1 && args[0].ToUpper() == "DIR") ||
(args.Length >= 2 && args.Length <= 3)))
{
DisplayUsage();
return 64;
}
// Initialize source and destination variables.
string source = null;
string destination = null;
if (args.Length > 1)
{
source = args[1];
destination = source;
if (args.Length == 3)
{
destination = args[2];
}
}
// Process command.
switch (args[0].ToUpper())
{
case "DIR":
ListFiles();
break;
case "GET":
GetFile(source, destination);
break;
default:
DisplayUsage();
break;
}
return 0;
}
private static void ListFiles()
{
WebDirectory webDir = new WebDirectory();
string[] files;
files = webDir.ListFiles();
foreach (string file in files)
{
Console.WriteLine(file);
}
Console.WriteLine("\n{0} file(s) in directory.", files.Length);
}
private static void GetFile(string source, string destination)
{
WebDirectory webDir = new WebDirectory();
WebFile webFile = new WebFile();
// Retrieve the requested file and then save it to disk.
webFile = webDir.GetFile(source);
// Save the retrieved web file to the file system.
FileStream fs = File.OpenWrite(destination);
fs.Write(webFile.Contents, 0, webFile.Contents.Length);
fs.Close();
// Set the date/time stamps for the file.
File.SetCreationTime(destination, webFile.CreationTime);
File.SetLastAccessTime(destination, webFile.LastAccessTime);
File.SetLastWriteTime(destination, webFile.LastWriteTime);
}
private static void DisplayUsage()
{
Console.WriteLine("WebFile is used to send and retrieve files from the WebDirectory web service.");
Console.WriteLine("\nUsage: WebFile command source [destination]");
Console.WriteLine("\tcommand - Either DIR or GET.");
Console.WriteLine("\tsource - The the name of the file to either retrieve or send.");
Console.WriteLine("\tdestination - Optionally the name of the destination file.");
Console.WriteLine("\nExamples:");
Console.WriteLine("\tWebFile GET somefile.exe");
Console.WriteLine("\tWebFile GET somefile.exe c:\temp");
Console.WriteLine("\tWebFile GET somefile.exe c:\temp\myfile.exe");
}
}
}
在VS2005测试通过。
[reference]“Building XML Web Services –For The Microsoft.NET Platform”(Scott Short)--构建XML Web服务——基于Microsoft .NET平台
整本书的代码可以从