Frage

The goal I have is simple: From a web app, write to a folder on the D drive of a local server. When I run the code locally (in the debugger) it writes to the folder beautifully. When I publish the code to the web server it cannot see its own D drive, or shared folder. I have tried every permutation of the file path string I can imagine, including absurd ones.

Examples:

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"

... and a host of others as well.

Using Response.Write statements to get a sense of what's happening, if I run the code locally, I get the following feedback:

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

And the file writes to the folder.

When I deploy that same code, I get a failing result:

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

No file is written.

I've gone to the folder and explicitly granted write permissions to all the users in our group who need it, thinking perhaps it was a badly reported permissions issue. No such luck.

One oddity I have noted is that the last line in my response.write gives me upper case letters for the domain user when I run it in the debugger, and lowercase when the code is deployed. I don't know that it should matter, but it does seem odd.

Any ideas why the code can see the shared folder from my debugger, but not when deployed?

For DJ KRAZE'S request below:

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

... and then...

// 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());
}
War es hilfreich?

Lösung 2

Traxs is correct. It did indeed turn out to be a permissions issue. We are interfacing two systems, and created a domain account named Interface.Dev. That user was listed in the security properties with write permissions, but for some reason needed Full Control rather than Write.

Thanks Traxs for persistently pointing me down that path. The error message was TERRIBLY misleading, and I would have continued fighting with the path information for an eternity otherwise.

Andere Tipps

Have you tried

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

?

It may be because you do not have write access to that path.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top