【问题标题】:Directory Not Found exception when deployed to server部署到服务器时未找到目录异常
【发布时间】:2013-08-02 13:32:22
【问题描述】:

我的目标很简单:从 Web 应用程序写入本地服务器 D 盘上的文件夹。当我在本地(在调试器中)运行代码时,它会漂亮地写入文件夹。当我将代码发布到 Web 服务器时,它看不到它自己的 D 驱动器或共享文件夹。我已经尝试了我能想象到的文件路径字符串的所有排列,包括荒谬的排列。

例子:

filepath = "\\\\wsbliwad\\d\\Payroll\\PaperlessPay";
filepath = "\\wsbliwad\\d\\Payroll\\PaperlessPay";
filepath = "\\wsbliwad\d\Payroll\PaperlessPay";
filepath = "\\\\wsbliwad\\Payroll\\PaperlessPay";
filepath = "\\wsbliwad\Payroll\PaperlessPay";
filepath = @"\\wsbliwad\payroll\PaperlessPay";
filepath = @"\\\\wsbliwad\\payroll\\PaperlessPay";
filepath = @"\\wsbliwad\\payroll\\PaperlessPay";
filepath = @"\\wsbliwad\d\Payroll\PaperlessPay"

...以及许多其他人。

使用 Response.Write 语句来了解正在发生的事情,如果我在本地运行代码,我会得到以下反馈:

Path one = \\wsbliwad\payroll\PaperlessPay
Exists = True
Path two = \\wsbliwad\\payroll\\PaperlessPay
Exists = True
Path one = \\\\wsbliwad\\payroll\\PaperlessPay
Exists = True
Host Name is CPU1476
AD User is ANVILCORP\DGray

文件写入文件夹。

当我部署相同的代码时,我得到一个失败的结果:

Path one = \\wsbliwad\payroll\PaperlessPay
Exists = False
Path two = \\wsbliwad\\payroll\\PaperlessPay
Exists = False
Path one = \\\\wsbliwad\\payroll\\PaperlessPay
Exists = False
Host Name is WSBLIWAD
AD User is ANVILCORP\dgray

没有文件被写入。

我已经进入该文件夹,并明确授予我们组中所有需要它的用户写入权限,我认为这可能是一个错误报告的权限问题。没有这样的运气。

我注意到的一个奇怪之处是,当我在调试器中运行它时,response.write 中的最后一行为域用户提供了大写字母,而在部署代码时为我提供了小写字母。我不知道这应该有什么关系,但它确实看起来很奇怪。

任何想法为什么代码可以从我的调试器中看到共享文件夹,但在部署时看不到?

以下 DJ KRAZE 的要求:

protected void btn_Export_Click(object sender, EventArgs e)
{

    DateTime mCheckDate = DateTime.Parse(tb_CheckDate.Text);
    int mPeriod = Int32.Parse(tb_Period.Text);


    try
    {
        string checkDateFormat = "MM/dd/yyyy";
        ReadOnlyCollection<IDataRow> dataRows = FlatFileExportWeb.PaperlessPay.GetPaperlessPayData(mPeriod.ToString(), mCheckDate.ToString(checkDateFormat));

        if (dataRows == null)
            return;

        IDataFormat format = new SimpleDataFormat(dataRows);

        string filepath = "";
        string machineName = System.Net.Dns.GetHostName();               
        if (machineName == "WSBLIWAD")
        {
            // This path does not work
            filepath = @"\\wsbliwad\d$\Payroll\PaperlessPay";
        }
        else
        {
            // this path works when debugging
            filepath = @"\\wsbliwad\payroll\PaperlessPay";
        }

        string filename = "PaperlessPay" + mCheckDate.ToString("MMddyyyy") + ".txt";

        new FileGenerator(filepath, filename).BuildFile(format);
        Response.Write("<br />&nbsp;&nbsp;&nbsp;Success!! The flat file has been written to " + filepath + @"\" + filename);
    }
    catch (Exception ex)
    {
        // Display any exceptions that may have been thrown.
        System.Web.HttpContext.Current.Response.Write(ex);
    }
}

...然后...

// Absolute path with concatenated filename
string mFilenameWithPath;

/// <summary>
/// Constructs a FileGenerator instance pointing to filepath\PaperlessPay\filename.
/// Will delete pre-existing file at this location.
/// </summary>
public FileGenerator(string filepath, string filename)
{
    if (!Directory.Exists(filepath))
        throw new DirectoryNotFoundException(filepath);

    mFilenameWithPath = filepath + @"\" + filename;

    if (File.Exists(mFilenameWithPath))
        File.Delete(mFilenameWithPath);
}


/// <summary>
/// Given an IDataFormat instance, BuildFile builds an output string.
/// It will then write this output string to the file specified within
/// the class, as passed into the constructor.
/// </summary>
/// <param name="format"></param>
public void BuildFile(IDataFormat format)
{
    // Make sure the format exists
    if (format == null)
        return;

    // Collect output string, and
    // write the string to filepath.
    using (StreamWriter writer = File.CreateText(mFilenameWithPath))
        writer.Write(format.Build());
}

【问题讨论】:

  • IIS 在您的目标服务器上以哪个用户身份运行?
  • 通过网络执行目录路径时,您需要在文件路径中使用$,例如\\servername\c$\FolderName Servername\DriveName\FolderName home,如果您通过网络应用程序执行此操作,您应该查看MapServerPath
  • @DJKRAZE 不适用于命名共享,您不需要;这仅适用于 C:、D: 等的管理共享。“$”仅表示它对任何浏览者都隐藏。
  • @DJGray 你为什么首先使用 UNC 路径与本地驱动器通信?
  • 是的 - 这听起来还是一个权限问题。

标签: c# asp.net intranet unc


【解决方案1】:

你试过了吗

 @"\\wsbliwad\d$\Payroll\PaperlessPay"

?

【讨论】:

  • 不,这是一个很好的建议。不幸的是,我仍然收到 DirectoryNotFoundException。
  • Hrm - 听起来像是权限问题。运行 IIS 的用户没有该文件夹的权限。
  • Traxs,我也有这样的想法,这就是为什么我在上面提到我明确授予需要访问这些文件夹的五个人的权限。这是多余的,因为它们已经在 AD 组中,并且这些组具有对该文件夹的写入权限。似乎我会收到一条不同的错误消息,但同样,我正在尝试我能想象的一切以使其正常工作,而权限是我想象的事情之一......
【解决方案2】:

Traxs 是正确的。这确实是一个权限问题。我们正在连接两个系统,并创建了一个名为 Interface.Dev 的域帐户。该用户在具有写入权限的安全属性中列出,但由于某种原因需要完全控制而不是写入。

感谢 Traxs 坚持不懈地为我指明这条道路。该错误消息具有极大的误导性,否则我会一直与路径信息抗争。

【讨论】:

    【解决方案3】:

    这可能是因为您没有对该路径的写入权限。

    【讨论】:

      猜你喜欢
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多