部署到服务器时发现目录未找到异常

时间:2013-08-02 13:32:23

标签: c# asp.net intranet unc

我的目标很简单:从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'S请求:

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

3 个答案:

答案 0 :(得分:3)

你试过吗

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

答案 1 :(得分:1)

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

感谢Traxs一直指着我走这条路。错误消息是可怕的误导性的,否则我会继续与路径信息争夺一段时间。

答案 2 :(得分:1)

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