我正在嘗試在C#中創建一個小程序,以便從Sharepoint數據庫中提取文件。我從在線搜索中獲取了代碼,並試圖對其進行編輯,以便將文件解壓縮到特定的文件夾,同時維護Sharepoint數據庫中的文件夾。從共享點數據庫中提取文件
具體來說,我希望用戶從表單中輸入服務器的名稱,他們試圖從中提取文件的數據庫的名稱,最後是他們希望用來提取文件的路徑。
我在調試時出現錯誤。
的錯誤狀態:Could not find a part of the path:
關於這行代碼:
FileStream fs = new FileStream(txtdir.Text + "/" + DirName + "/" + LeafName,
FileMode.Create, FileAccess.Write);
這裏是我的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace SPEXTRACTOR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ENTER_Click(object sender, EventArgs e)
{
string DBConnString = "Server=" + txtserver.Text + ";Database="
+ txtDB.Text + ";Trusted_Connection=True;";
SqlConnection con = new SqlConnection(DBConnString);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT ad.SiteId, ad.Id, ad.DirName," +
" ad.LeafName, ads.Content" +
" FROM AllDocs ad, AllDocStreams ads" +
" WHERE ad.SiteId = ads.SiteId" +
" AND ad.Id = ads.Id" +
" AND ads.Content IS NOT NULL" +
" Order by DirName";
//SqlCommand com = con.CreateCommand();
//com.CommandText = "select DirName, LeafName, Content
from AllDocStreams, AllDocs
where (LeafName like '%.doc' or LeafName like '%.xls'
or LeafName like '%.pdf' or LeafName like '%.ppt')
and Content is not NULL";
//com.CommandText = "select DirName, LeafName, Content
from AllDocStreams, AllDocs
where (LeafName like '%.doc' or LeafName like '%.xls'
or LeafName like '%.pdf' or LeafName like '%.ppt')
and Content is not NULL";
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
// grab the file's directory and name
string DirName = (string)reader["DirName"];
string LeafName = (string)reader["LeafName"];
if (!Directory.Exists(DirName))
{
Directory.CreateDirectory(DirName);
Console.WriteLine("Creating directory: " + DirName);
}
//FileStream fs = new FileStream(DirName + "/" + LeafName
, FileMode.Create, FileAccess.Write);
FileStream fs = new FileStream(txtdir.Text + "/" + DirName + "/"
+ LeafName, FileMode.Create, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
// depending on the speed of your network,
//you may want to change the buffer size (it's in bytes)
int bufferSize = 1000000;
long startIndex = 0;
long retval = 0;
byte[] outByte = new byte[bufferSize];
// grab the file out of the db one chunk (of size bufferSize) at a time
do
{
retval = reader.GetBytes(4, startIndex, outByte, 0, bufferSize);
//retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize);
startIndex += bufferSize;
writer.Write(outByte, 0, (int)retval);
writer.Flush();
} while (retval == bufferSize);
// finish writing the file
writer.Close();
fs.Close();
Console.WriteLine("Finished writing file: " + LeafName);
}
// close the DB connection and whatnots
reader.Close();
con.Close();
}
}
}
可以調試或寫出來的值是什麼您預期的完整路徑的對問題的行? – 2011-04-19 16:56:15
第一次迭代。 – Jeff 2011-04-19 17:00:21
示例:如果我想將文件解壓縮到c:\ aa – Jeff 2011-04-19 17:00:43