5

我正在尝试将我的 c# 代码生成的文件(Excel 文件)复制到我可以访问的远程网络路径中,如下所示:

string folder = "\\\\testing-path\\Audit\\Reports";
if (!(Directory.Exists(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit")))
{
    Directory.CreateDirectory(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit");
}
folder = folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit";

if (File.Exists(folder + "\\Audit- " + fname + ".xlsx"))
{
    File.Delete(folder + "\\Audit- " + fname + ".xlsx");
}
string fileName = folder + "\\Audit- " + fname + ".xlsx";
wb.SaveAs(fileName,
    Excel.XlFileFormat.xlWorkbookDefault, null, null,
    false, false, Excel.XlSaveAsAccessMode.xlNoChange,
    null, null, null, null, null);

此代码可以正常工作 8/10 次,并引发其他 2 次网络错误(未找到网络路径)。当远程路径抛出此错误时,我在机器上使用运行命令打开上述远程路径,我运行此代码,并且可以正常访问它。关闭手动打开的文件夹并重新运行代码可以解决问题。可能是什么问题?我在这里做错了吗?

PS:我还尝试将 excel 文件保存到桌面上然后使用File.Copy,这种间歇性行为没有改变。

4

1 回答 1

1

检查以下事项

1) First you have to check whether machine is on or off
2) Then check you have access rights to put the file in that particular folder and dont
   put it inside ' C ' Drive because C is not accessible for other users in network put it inside D or E drive.
3) If Folder is Missing You have to Create A Folder first

什么是'WB'

 if(!Directory.Exists("\\\\testing-path\\Audit\\Reports"))
    Directory.Create("\\\\testing-path\\Audit\\Reports");

利用

                                                          FilePath     Byte 

 File.ReadAllBytes("FilePath/FileName.Extension",byte) //  D:\\Test.xls,12878

然后在远程位置 FilePath 字节

 File.WriteAllBytes("FilePath/FileName.Extension",byte)//   D:\\Test123.xls,12878

检查此链接

访问远程机器中的路径:http: //www.codeproject.com/Questions/184633/Connect-to-a-shared-folder-using-ip-address-in-vb6

于 2013-10-08T06:38:08.777 回答