c# - 部署到服务器时出现目录未找到异常

标签 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 的以下请求:

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

最佳答案

你试过吗

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

关于c# - 部署到服务器时出现目录未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18018196/

相关文章:

javascript - 单击按钮发送 Ajax 调用并从 aspx.cs(代码隐藏)获取值到 aspx 页面并将其显示在同一页面的输入字段中

html - 如何在 Mitel Unified Communicator Advanced 的 Intranet 上标记电话号码

c# - Unity Engine - 通过碰撞实例化预制件

ASP.NET Ajax 错误 Sys.WebForms.PageRequestManagerServerErrorException : An unknown error occurred while processing the request on the server

c# - 使用父级从组件 Blazor 调用方法

c# - CheckBoxList 项目已选中事件

email - 来自 WebSite 的 EWS 使用内部网的用户凭据和 Windows 身份验证

apache - 如何防止Internet Explorer在Domain匹配AD-Domain时以Intranet模式显示页面

c# - SQL 语法错误

c# - System.Array 不包含 'Any' 的定义 - C#