【发布时间】: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 /> 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$\FolderNameServername\DriveName\FolderNamehome,如果您通过网络应用程序执行此操作,您应该查看MapServerPath -
@DJKRAZE 不适用于命名共享,您不需要;这仅适用于 C:、D: 等的管理共享。“$”仅表示它对任何浏览者都隐藏。
-
@DJGray 你为什么首先使用 UNC 路径与本地驱动器通信?
-
是的 - 这听起来还是一个权限问题。