詳解C#對(duì)路徑...的訪問被拒絕解決過程
用C#想寫一個(gè)直接將數(shù)據(jù)庫查詢得到的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; //由于我的查詢語句較長,采用了讀取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)”語句處發(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)文章
C#程序終極調(diào)試實(shí)現(xiàn)windbg的時(shí)間旅行
這篇文章主要介紹了C#程序終極調(diào)試實(shí)現(xiàn)windbg的時(shí)間旅行示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06C#入門學(xué)習(xí)之集合、比較和轉(zhuǎn)換
本文詳細(xì)講解了C#中的集合、比較和轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#創(chuàng)建WebService接口并連接的全過程
工作時(shí)遇到需要請(qǐng)求客戶的接口返回?cái)?shù)據(jù),要求使用WebService,借此機(jī)會(huì)記錄一下,下面這篇文章主要給大家介紹了關(guān)于C#創(chuàng)建WebService接口并連接的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法)
本文主要介紹了C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11C#中委托和事件在觀察者模式中的應(yīng)用實(shí)例
這篇文章主要介紹了C#中委托和事件在觀察者模式中的應(yīng)用,需要的朋友可以參考下2014-08-08