我的visual studio项目存储在C:\Projects
中,项目名称为Project1
,文件名为Test.xls
string directory = AppDomain.CurrentDomain.BaseDirectory;
// directory = C:\\Projects\\Project1 (local drive)
directory = directory.Remove(0, 2);
// directory = \\Projects\\Project1
string newDirectory = "\\server" + directory;
// newDirectory = \\server\\Projects\\Project1 (now, network shared drive)
string pathCreate = System.IO.Path.Combine(newDirectory, file);
// pathCreate = \\server\\Projects\\Project1\\Test.xls
string excelConStr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + pathCreate + @"; Extended Properties=""Excel 8.0;HDR=Yes; MultipleActiveResultSets=True"";";
private void CreateExcel(string excelConStr)
{
DataTable table = new DataTable();
using (OleDbConnection excelCreateCon = new OleDbConnection(excelConStr))
{
OleDbCommand excelCreateCmd = new OleDbCommand();
excelCreateCmd.Connection = excelCreateCon
...
使用debug,excelConStr返回
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\server\\Projects\\Project1\\Test.xls; Extended Properties=\"Excel 8.0;HDR=Yes; MultipleActiveResultSets=True\";"
excelCreateCmd.Connection.DataSource returns "\\server\\Projects\\Project1\\Test.xls"
看起来很好。但是,它给了我以下错误:'C:\server\Projects\Project1\Test.xls' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
有任何解决此问题的建议吗?请高手!! (注意:没有PERMISSON问题)
答案 0 :(得分:0)
对于网络,我认为你不需要\ server而不是\\ server,不是吗?