C# 从网络服务写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/233681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write a file from webservices
提问by user23149
I have a web service using .net c# and I want to write to a text file on the server, but I cannot get this to work. I believe it's a permission problem.
我有一个使用 .net c# 的网络服务,我想写入服务器上的文本文件,但我无法让它工作。我相信这是一个许可问题。
Specifically, I think the problem is I am using System.IO.Directory.GetCurrentDirectory()
.
具体来说,我认为问题在于我正在使用System.IO.Directory.GetCurrentDirectory()
.
Is there a better alternative?
有更好的选择吗?
回答by StingyHyman
Try granting the ASP.NET user (or whatever account IIS is running as) permission to write to the folder you are trying to write to.
尝试授予 ASP.NET 用户(或 IIS 运行的任何帐户)写入您尝试写入的文件夹的权限。
If this is a network share, try to run IIS as a domain user that can write to the share.
如果这是网络共享,请尝试以可以写入共享的域用户身份运行 IIS。
Remember the principle of granting minimal permission (dont use Admin level access).
记住授予最小权限的原则(不要使用管理员级别的访问权限)。
回答by Jason Whitehorn
You should be able to write files from web services. This is most likely a permissions or trust issue. If you are in a limited trust (i.e., Medium trust) ensure that you are writing to a patch in or below your web root. If you are already doing that, or are in a full trust environment check to make sure that the directory has permissions for the IIS worker process to write to it.
您应该能够从 Web 服务写入文件。这很可能是权限或信任问题。如果您处于有限信任(即中等信任)中,请确保您正在写入 Web 根目录中或下方的补丁。如果您已经这样做了,或者处于完全信任的环境中,请检查以确保该目录具有 IIS 工作进程写入的权限。
回答by Kev
If you're running on Windows 2003 and haven'tturned on ASP.NET impersonation, and are running the app in the DefaultAppPool or an application pool that is configured to run under the identity of "Network Service", then you'll need to give the "Network Service" account write permission to the destination folder. If you're running the site in an app pool that is using an identity other than "Network Service" then that account may require write permissions to the destination folder.
如果您在 Windows 2003 上运行并且没有打开 ASP.NET 模拟,并且正在 DefaultAppPool 或配置为在“网络服务”身份下运行的应用程序池中运行应用程序,那么您需要授予“网络服务”帐户写入目标文件夹的权限。如果您在使用“网络服务”以外的身份的应用程序池中运行该站点,则该帐户可能需要对目标文件夹的写入权限。
If you're running windows 2000 then the '<MACHINENAME>\ASPNET'
account will need write permissions to the destination folder.
如果您运行的是 Windows 2000,则该'<MACHINENAME>\ASPNET'
帐户将需要对目标文件夹的写入权限。
If you've got impersonation turned on then you'll need to give the site's anonymous user account write permissions to the destination folder instead.
如果您打开了模拟功能,则需要为该站点的匿名用户帐户授予对目标文件夹的写入权限。
To check if impersonation is turned on, open (assuming ASP.NET 2.0) then check your machine.config file (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) to see if you have the following setting:
要检查模拟是否已打开,请打开(假设为 ASP.NET 2.0)然后检查您的 machine.config 文件 (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) 以查看您是否具有以下设置:
<identity impersonate="true"/>
This may also be overridden in your application's web.config.
这也可能在您的应用程序的 web.config 中被覆盖。
Additionally if you're running in a partial trust environment then you'll likely only be able to write to the website's application folder because the default FileIOPermission is usually set to $AppDir$, i.e. you can't modify files anywhere else, even with the correct NTFS permissions.
此外,如果您在部分信任环境中运行,那么您可能只能写入网站的应用程序文件夹,因为默认的 FileIOPermission 通常设置为 $AppDir$,即您不能在其他任何地方修改文件,即使使用正确的 NTFS 权限。
If you're writing to a network share then StingyHymanhas the answer for you, but the partial trust environment considerations still apply.
如果您正在写入网络共享,那么StingyHyman可以为您提供答案,但部分信任环境的考虑仍然适用。
But check your NTFS perms first, that's probably the best bet.
但首先检查您的 NTFS 权限,这可能是最好的选择。
Hope this helps Kev
希望这有助于凯夫
回答by Austin
Stuart, instead of using System.IO.Directory.GetCurrentDirectory(), you may want to use Server.MapPath. You can give Server.MapPath the directory relative to the location of your web service where you want to save the file, otherwise you probably need to pass a full file path "C:\Files\file.txt". As far as the permissions issue, I am usually able to resolve this by adding write access to the folder I'm writing the file to for IIS_WPG and ASPNET users (These usernames may be different on your server).
Stuart,您可能想要使用 Server.MapPath,而不是使用 System.IO.Directory.GetCurrentDirectory()。您可以为 Server.MapPath 提供与您要保存文件的 Web 服务位置相关的目录,否则您可能需要传递完整的文件路径“C:\Files\file.txt”。至于权限问题,我通常可以通过为 IIS_WPG 和 ASPNET 用户(这些用户名在您的服务器上可能不同)添加对我正在写入文件的文件夹的写访问权限来解决此问题。
回答by simaglei
If you don't specify a destination folder I assume your web service wants to write to "C:\Windows\System32\" or something of that kind. That's why a UnauthorizedAccessException will be thrown on the server. In order to write to the "home"-directory of the web service you have to find out where that is first.
如果您没有指定目标文件夹,我假设您的 Web 服务想要写入“C:\Windows\System32\”或类似的内容。这就是为什么会在服务器上抛出 UnauthorizedAccessException 的原因。为了写入 Web 服务的“home”目录,您必须首先找出它的位置。
The following works for me but you also find other suggestions here.
以下内容对我有用,但您也可以在此处找到其他建议。
Add the reference System.Web, and get the current directory by calling:
添加引用 System.Web,并通过调用获取当前目录:
strFileDestination = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + strFileName;
Remember to give your service write permission in IIS.
请记住在 IIS 中授予您的服务写入权限。