詳解C#對(duì)路徑...的訪問被拒絕解決過程
用C#想寫一個(gè)直接將數(shù)據(jù)庫(kù)查詢得到的datatable,直接導(dǎo)出為csv格式的文件,拷貝到導(dǎo)出的操作類后,一直catch到的錯(cuò)誤提示是對(duì)路徑的泛微被拒絕,一直排查原因,發(fā)現(xiàn)原來:FileStream(path, FileMode.OpenOrCreate,FileAccess.ReadWrite),path處所讀取的字符串必須包含文件名稱以及格式?,F(xiàn)在貼完整代碼,以供幫助到像我一樣的初學(xué)者。
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamReader st;
//由于我的查詢語(yǔ)句較長(zhǎng),采用了讀取txt文本的方式后做查詢操作。
st = new System.IO.StreamReader(Application.StartupPath + "\\SQL2.txt", System.Text.Encoding.Default);
string stingsql=st.ReadToEnd();
st.Close();
textBox1.Text = stingsql;
DataTable dt = new DataTable();
dt = bc.QueryCommand(stingsql);
string filepath = @"F:\病案導(dǎo)出備份\患者統(tǒng)計(jì)表.csv";//此處必須為路徑加文件名稱,否則
ImportToCSV(dt, filepath);
}
public static void ImportToCSV(DataTable dt, string filepath)
{
FileStream fs = null;
StreamWriter sw = null;
try
{
fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.Default);
string head = "";
//拼接列頭
for (int cNum = 0; cNum < dt.Columns.Count; cNum++)
{
head += dt.Columns[cNum].ColumnName + ",";
}
//csv文件寫入列頭
sw.WriteLine(head);
string data = "";
//csv寫入數(shù)據(jù)
for (int i = 0; i < dt.Rows.Count; i++)
{
string data2 = string.Empty;
//拼接行數(shù)據(jù)
for (int cNum1 = 0; cNum1 < dt.Columns.Count; cNum1++)
{
data2 = data2 + "\"" + dt.Rows[i][dt.Columns[cNum1].ColumnName].ToString() + "\",";
}
bool flag = data != data2;
if (flag)
{
sw.WriteLine(data2);
}
data = data2;
}
string msg = "數(shù)據(jù)被成功導(dǎo)出到:" + filepath;
MessageBox.Show(msg);
}
catch (Exception ex)
{
// logger.Error("導(dǎo)出csv失?。? + ex.Message);
MessageBox.Show("導(dǎo)出失敗" + ex.Message);
return;
}
finally
{
if (sw != null)
{
sw.Close();
}
if (fs != null)
{
fs.Close();
}
sw = null;
fs = null;
}
}
示例2
問題代碼:
private bool GetChannelInfo()
{
comCheckWindow.LoadCheckResult("準(zhǔn)備加載項(xiàng)目通道信息", Color.FromName("Green"));
XmlDocument proFile = new XmlDocument(); //讀取項(xiàng)目配置文件
proFile.Load(proFilePath);
XmlNodeList channelList = proFile.SelectSingleNode("Project").ChildNodes;
if (channelList.Count == 0) return false;
......
return true;
}
在“proFile.Load(proFilePath)”語(yǔ)句處發(fā)生錯(cuò)誤,提示對(duì)路徑…(proFilePath的值)的訪問被拒絕。
嘗試過將目標(biāo)文件重新選擇路徑(從C盤轉(zhuǎn)移到D盤),或提升程序運(yùn)行權(quán)限(在以管理員身份運(yùn)行Visual Studio的情況下打開項(xiàng)目文件),均無效。
最后檢查程序時(shí)發(fā)現(xiàn):路徑proFilePath的值不正確,運(yùn)行“proFile.Load(proFilePath)”要求路徑proFilePath指向一個(gè)確定的XML文件,但此處路徑的值為該XML文件所在目錄的路徑,由于Load函數(shù)的參數(shù)指向?qū)ο箢愋筒黄ヅ?,從而?dǎo)致出錯(cuò)。
到此這篇關(guān)于詳解C#對(duì)路徑...的訪問被拒絕解決過程的文章就介紹到這了,更多相關(guān)C# 路徑訪問被拒絕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
超簡(jiǎn)單C#獲取帶漢字的字符串真實(shí)長(zhǎng)度(單個(gè)英文長(zhǎng)度為1,單個(gè)中文長(zhǎng)度為2)
正常情況下,我們是直接去string的length的,但是漢字是有兩個(gè)字節(jié)的,所以直接用length是錯(cuò)的2018-03-03
C#實(shí)現(xiàn)多線程的幾種方式常用場(chǎng)景分析
多線程是C#中一個(gè)重要的概念,多線程指的是在同一進(jìn)程中同時(shí)運(yùn)行多個(gè)線程的機(jī)制,多線程適用于需要提高系統(tǒng)并發(fā)性、吞吐量和響應(yīng)速度的場(chǎng)景,可以充分利用多核處理器和系統(tǒng)資源,提高應(yīng)用程序的性能和效率,這篇文章主要介紹了C#實(shí)現(xiàn)多線程的幾種方式,需要的朋友可以參考下2024-05-05
c#下注冊(cè)表操作的一個(gè)小細(xì)節(jié)
c#下注冊(cè)表操作的一個(gè)小細(xì)節(jié)...2007-11-11
C#實(shí)現(xiàn)批量Word轉(zhuǎn)換Html的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#批量Word轉(zhuǎn)換Html的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

