c# - 为什么我会收到 "Could not find a part of the path"异常?

标签 c# asp.net visual-studio-2010 filenotfoundexception

我正在使用 Visual Studio 2010 开发网站。我正在尝试将文件保存在路径中。它在本地主机上运行良好。

但是相同的代码在 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'.

代码如下:

protected void btnImportFile_Click(object sender, EventArgs e)
{
    sArReportText = File.ReadAllText(txtFilePath.Text.Trim());
    // Set the report Properties to insert Report information
    SetProperties();
}

最佳答案

您可能也遇到了我的情况:目录名称包含一些不寻常的字符。就我而言,

Could not find a part of the path 'C:\Web\metBoot\wild iis\DigiCert© Certificate Utility for Windows_files'.

版权标志就是问题所在。

因此使用来自 Obtaining the short 8.3 filename from a long filename 的概念,我首先将我的路径转换为短格式,然后使用它来获取我的文件列表。

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);

关于c# - 为什么我会收到 "Could not find a part of the path"异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18740564/

相关文章:

c# - 将表单上的控件移动到选项卡上

c# - 将值从 aspx 传递到 javascript

c# - MessageBox 在 SaveFileDialog 之后不显示(聚焦)

c++ - 在 Visual Studio 2010 中测量 C++ 项目性能的问题

c++ - 创建dll文件是否一定要写头文件?

C#随机数不生成0

c# - 如何删除路径中的空格?

c# bool 运算符 - 正常与短路

asp.net - 从 ASP.NET Core 2.2 迁移到 3.1 时 services.BuildServiceProvider(在ConfigureServices 中)的替代代码

c# - error 该进程无法访问该文件,因为它正被另一个进程使用