我的目标很简单:从 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());
}