C# 部署到服务器时未找到目录异常

C# 部署到服务器时未找到目录异常,c#,asp.net,intranet,unc,C#,Asp.net,Intranet,Unc,我的目标很简单:从一个web应用程序,写入本地服务器D驱动器上的文件夹。当我在本地(调试器中)运行代码时,它会漂亮地写入文件夹。当我将代码发布到web服务器时,它无法看到自己的D驱动器或共享文件夹。我尝试了我能想象到的所有文件路径字符串的排列,包括荒谬的排列 示例: filepath = "\\\\wsbliwad\\d\\Payroll\\PaperlessPay"; filepath = "\\wsbliwad\\d\\Payroll\\PaperlessPay"; filepath = "

我的目标很简单:从一个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);
    }
}
受保护的无效btn\u导出\u单击(对象发送方,事件参数e)
{
DateTime mCheckDate=DateTime.Parse(tb_CheckDate.Text);
intmperiod=Int32.Parse(tb_Period.Text);
尝试
{
字符串checkDateFormat=“MM/dd/yyyy”;
ReadOnlyCollection dataRows=FlatFileExportWeb.PaperlessPay.GetPaperlessPayData(mPeriod.ToString(),mCheckDate.ToString(checkDateFormat));
if(dataRows==null)
返回;
IDataFormat=新的SimpleDataFormat(数据行);
字符串filepath=“”;
字符串machineName=System.Net.Dns.GetHostName();
if(machineName==“WSBLIWAD”)
{
//这条路行不通
filepath=@“\\wsbliwad\d$\Payroll\PaperlessPay”;
}
其他的
{
//此路径在调试时起作用
文件路径=@“\\wsbliwad\payroll\PaperlessPay”;
}
字符串filename=“PaperlessPay”+mCheckDate.ToString(“MMddyyyy”)+“.txt”;
新文件生成器(文件路径,文件名).BuildFile(格式);
Response.Write(“
成功!!平面文件已写入”+filepath+@“\”+filename); } 捕获(例外情况除外) { //显示可能已引发的任何异常。 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());
}
//带有串联文件名的绝对路径
字符串mFilenameWithPath;
/// 
///构造指向filepath\PaperlessPay\filename的FileGenerator实例。
///将删除此位置预先存在的文件。
/// 
公共文件生成器(字符串文件路径、字符串文件名)
{
如果(!Directory.Exists(filepath))
抛出新的DirectoryNotFoundException(文件路径);
mFilenameWithPath=文件路径+@“\”+文件名;
if(File.Exists(mFilenameWithPath))
File.Delete(mFilenameWithPath);
}
/// 
///给定IDataFormat实例,BuildFile将生成一个输出字符串。
///然后,它会将此输出字符串写入在中指定的文件
///传递到构造函数中的类。
/// 
/// 
公共void构建文件(IDataFormat格式)
{
//确保该格式存在
if(格式==null)
返回;
//收集输出字符串,然后
//将字符串写入filepath。
使用(StreamWriter=File.CreateText(mFilenameWithPath))
writer.Write(format.Build());
}
您试过了吗

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

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


谢谢Traxs一直给我指出这条路。这个错误消息具有严重的误导性,否则我将永远与路径信息作斗争。

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

IIS在您的目标服务器上作为哪个用户运行?在网络上执行目录路径时,您需要利用文件路径中的
$
\\servername\c$\FolderName
servername\DriveName\FolderName
主页如果您通过web应用程序执行此操作,那么您应该查看
MapServerPath
@DJKRAZE不用于命名共享,您不需要;这仅适用于C:,D:,等的管理共享。“$”只意味着它对任何浏览者都是隐藏的。@DJGray为什么您首先使用UNC路径与本地驱动器对话?是的-这听起来仍然像是权限问题。不,这是一个很好的建议。不幸的是,我仍然得到DirectoryNotFoundException.Hrm-听起来像是权限问题。用户IIS正在运行,因为它没有访问该文件夹的权限。Traxs,我也有这样的想法,这就是为什么我在上面提到,我明确授予了需要访问这些文件夹的五个人权限。这是多余的,因为它们已经在广告组中,并且这些组具有对文件夹的写入权限。看起来我会收到一条不同的错误消息,但是,我再次尝试了我能想象到的一切,以便让它工作,权限是我想象的事情之一。。。