這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用C#無(wú)法訪問(wèn)路徑如何解決,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
用C#想寫(xiě)一個(gè)直接將數(shù)據(jù)庫(kù)查詢(xún)得到的datatable,直接導(dǎo)出為csv格式的文件,拷貝到導(dǎo)出的操作類(lèi)后,一直catch到的錯(cuò)誤提示是對(duì)路徑的泛微被拒絕,一直排查原因,發(fā)現(xiàn)原來(lái):FileStream(path, FileMode.OpenOrCreate,FileAccess.ReadWrite),path處所讀取的字符串必須包含文件名稱(chēng)以及格式。現(xiàn)在貼完整代碼,以供幫助到像我一樣的初學(xué)者。
private void button1_Click(object sender, EventArgs e) { System.IO.StreamReader st; //由于我的查詢(xún)語(yǔ)句較長(zhǎng),采用了讀取txt文本的方式后做查詢(xún)操作。 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";//此處必須為路徑加文件名稱(chēng),否則 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文件寫(xiě)入列頭 sw.WriteLine(head); string data = ""; //csv寫(xiě)入數(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
問(wèn)題代碼:
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的值)的訪問(wèn)被拒絕。
嘗試過(guò)將目標(biāo)文件重新選擇路徑(從C盤(pán)轉(zhuǎn)移到D盤(pán)),或提升程序運(yùn)行權(quán)限(在以管理員身份運(yùn)行Visual Studio的情況下打開(kāi)項(xiàng)目文件),均無(wú)效。
最后檢查程序時(shí)發(fā)現(xiàn):路徑proFilePath的值不正確,運(yùn)行“proFile.Load(proFilePath)”要求路徑proFilePath指向一個(gè)確定的XML文件,但此處路徑的值為該XML文件所在目錄的路徑,由于Load函數(shù)的參數(shù)指向?qū)ο箢?lèi)型不匹配,從而導(dǎo)致出錯(cuò)。
上述就是小編為大家分享的使用C#無(wú)法訪問(wèn)路徑如何解決了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:使用C#無(wú)法訪問(wèn)路徑如何解決-創(chuàng)新互聯(lián)
文章位置:http://aaarwkj.com/article18/cdhsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、建站公司、網(wǎng)站策劃、網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容