C# 为什么我会收到“找不到路径的一部分”异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18740564/
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
Why am I getting a "Could not find a part of the path" exception?
提问by Shashank
I am developing website using Visual Studio 2010
. I am trying to save a file in a path. It works fine localhost.
我正在使用Visual Studio 2010
. 我正在尝试将文件保存在路径中。它工作正常本地主机。
But the same code is not working in IIS. It shows the following error
但是相同的代码在 IIS 中不起作用。它显示以下错误
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Inetpub\wwwroot\Vendor\cn.jpg'.
Could not find a part of the path 'C:\Users\shashank\Desktop\ab.csv'.
异常详细信息:System.IO.DirectoryNotFoundException:找不到路径“C:\Inetpub\wwwroot\Vendor\cn.jpg”的一部分。
找不到路径“C:\Users\shashank\Desktop\ab.csv”的一部分。
Here is the code:
这是代码:
protected void btnImportFile_Click(object sender, EventArgs e)
{
sArReportText = File.ReadAllText(txtFilePath.Text.Trim());
// Set the report Properties to insert Report information
SetProperties();
}
回答by Bibhu
You need to have permissions set in iis to allow files to be saved in the folder. Basically your uploaded files should be saved inside a separate folder present inside your root directory.
您需要在 iis 中设置权限以允许将文件保存在文件夹中。基本上你上传的文件应该保存在你的根目录中的一个单独的文件夹中。
回答by Robin Joseph
This may be because, you are not having the specified file in web server, or you may be used an incorrect path. Specify the exact folder and filename as how it is stored in the web server. use HttpContext.Current.Request.ApplicationPath
or Server.MapPath
to specify the correct location where your desired file lies. And also make sure that you have given read and write permissions for this specific file and its folder.
这可能是因为,您在 Web 服务器中没有指定的文件,或者您可能使用了不正确的路径。指定确切的文件夹和文件名作为它在 Web 服务器中的存储方式。使用HttpContext.Current.Request.ApplicationPath
或Server.MapPath
指定所需文件所在的正确位置。还要确保您已授予此特定文件及其文件夹的读写权限。
回答by JulyOrdinary
In order to access, create and delete files on the server, must have rights. Like in my project I am using Impersonator class to access various files and folder from the server. Otherwise it will throw an exception.
为了访问、创建和删除服务器上的文件,必须有权限。就像在我的项目中一样,我使用 Impersonator 类从服务器访问各种文件和文件夹。否则会抛出异常。
回答by christiandev
It's usually best practice to use the App_Data
folder to save files to.
通常最好的做法是使用App_Data
文件夹来保存文件。
Take a look here, Working with files, for a tutorial.
查看此处使用文件,获取教程。
回答by jmcclure
You could use code impersonation:
您可以使用代码模拟:
http://csharptuning.blogspot.com/2007/06/impersonation-in-c.htmlhttp://www.codeproject.com/Articles/14358/User-Impersonation-in-NET
http://csharptuning.blogspot.com/2007/06/impersonation-in-c.html http://www.codeproject.com/Articles/14358/User-Impersonation-in-NET
regardless, whomever you use as the impersonation must be able to read/write to the location that is being saved to. We use this method in applications for delete/create folders across network. Even if App_Data is best practice, it may be a business requirement to access the documents outside of that folder.
无论如何,您用作模拟的任何人都必须能够读取/写入要保存到的位置。我们在应用程序中使用这种方法来跨网络删除/创建文件夹。即使 App_Data 是最佳实践,访问该文件夹外的文档也可能是业务需求。
You can also set impersonation on IIS.
您还可以在 IIS 上设置模拟。
I also notice that your function is called btnImportFile. You may want to look into FileUpload control if you are uploading a file, which allows you to get the byte array of the file and save as needed. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload%28v=vs.110%29.aspx. You might still need to use Server.MapPath or HttpContext.Current.Request.ApplicationPath depending on your needs.
我还注意到您的函数名为 btnImportFile。如果您正在上传文件,您可能需要查看 FileUpload 控件,它允许您获取文件的字节数组并根据需要进行保存。 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload%28v=vs.110%29.aspx。根据您的需要,您可能仍需要使用 Server.MapPath 或 HttpContext.Current.Request.ApplicationPath。
回答by bugmagnet
You might also be experiencing what I am: that the directory name contains some unusual characters. In my case,
您可能也遇到了我的情况:目录名称包含一些不寻常的字符。就我而言,
Could not find a part of the path 'C:\Web\metBoot\wild iis\DigiCert? Certificate Utility for Windows_files'.
That copyright sign is the issue.
那个版权标志就是问题所在。
So using concepts drawn from Obtaining the short 8.3 filename from a long filename, I convert my paths to short form first, then use that to get my list of files.
因此,使用从长文件名中获取短 8.3 文件名中得出的概念,我首先将路径转换为短格式,然后使用它来获取我的文件列表。
StringBuilder sf = new StringBuilder(300);
int n = GetShortPathName(sourceFolder, sf, 300);
if (0 == n)
{
tk.write(Marshal.GetLastWin32Error().ToString());
continue;
}
...
...
IEnumerable<string> fileGroup = Directory.EnumerateFiles(sf.ToString(), ext);
回答by Ben Power
Consider how you're launching VS too. Counter-intuitively I run into this problem only when I'm running VS in Administrator mode. Possibly a group policies thing.
还要考虑如何启动 VS。与直觉相反,我只有在以管理员模式运行 VS 时才会遇到这个问题。可能是组策略的事情。