PDA

View Full Version : سوال: خطای دسترسی به فولدر موقع ذخیره سازی



MasoudAdmin
شنبه 10 مهر 1400, 22:26 عصر
با سلام
من یک کد نوشتم که فایل Word رو از دیتابیس بخونه و بعد پارامترها رو Replace کنه و در فولدر MyDocument ویندوز ذخیره کنه. ولی موقع ذخیره شدن در فولدری که اعلام کردم خطای دسترسی میده. با اینکه دسترسی Full Control هم به فولدر دادم بازم خطا میده. حتی فولدر دستی تو درایو دیگه هم تعریف کردم و دسترسی Full Control هم دادم ولی باز هم همون خطا رو داره.


mydocumentsPath= Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + "\\Guarantee";




if (!System.IO.Directory.Exists(mydocumentsPath))
{
System.IO.Directory.CreateDirectory(mydocumentsPat h);
Random myrandom = new Random();
int MyValue = myrandom.Next();
string sFileName = mydocumentsPath + "\\tmp" + MyValue.ToString().Trim() + Extention;
byte[] filebyte;
CmdMain.CommandText = "Select LetterBody From VW_LetterSubject Where LetterCode=" + LetterCode;
filebyte = (byte[])CmdMain.ExecuteScalar();
string sTempFileName = Application.StartupPath + sFileName;
FileStream FS = new FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write);
FS.Write(filebyte, 0, filebyte.Length);
FS.Flush();
FS.Close();
Document document = new Document();
document.LoadFromFile(sFileName);
Dictionary<string, string> dictReplace = GetReplaceDictionary();
//Replace text
foreach (KeyValuePair<string, string> kvp in dictReplace)
{
document.Replace(kvp.Key, kvp.Value, true, true);
}
//Save doc file.
document.SaveToFile(mydocumentsPath, FileFormat.Docx);
document.Close();
ConData.Close();
}
else
{
foreach (string strFiles in System.IO.Directory.GetFiles(mydocumentsPath))
{
System.IO.File.Delete(strFiles);
}
Random myrandom = new Random();
int MyValue = myrandom.Next();
string sFileName = mydocumentsPath + "\\tmp" + MyValue.ToString().Trim() + Extention;
byte[] filebyte;
CmdMain.CommandText = "Select LetterBody From VW_LetterSubject Where LetterCode=" + LetterCode;
filebyte = (byte[])CmdMain.ExecuteScalar();
string sTempFileName = Application.StartupPath + sFileName;
FileStream FS = new FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write);
FS.Write(filebyte, 0, filebyte.Length);
FS.Flush();
FS.Close();
Document document = new Document();
document.LoadFromFile(sFileName);
Dictionary<string, string> dictReplace = GetReplaceDictionary();
//Replace text
foreach (KeyValuePair<string, string> kvp in dictReplace)
{
document.Replace(kvp.Key, kvp.Value, true, true);
}
//Save doc file.
document.SaveToFile(mydocumentsPath, FileFormat.Docx);
document.Close();
ConData.Close();
}


اینم کدهای من هست. ولی این خطا رو میده:
153502
ممنون میشم راهنمایی کنید.

MasoudAdmin
چهارشنبه 14 مهر 1400, 15:51 عصر
سلام. کسی نمی تونه کمکی بهم بکنه؟:ناراحت::افسرده:

MasoudAdmin
پنج شنبه 15 مهر 1400, 09:24 صبح
سلام
دوستان خودم حلش کردم. اینم کدش:


if (!System.IO.Directory.Exists(mydocumentsPath))
{
System.IO.Directory.CreateDirectory(mydocumentsPat h);
Random myrandom = new Random();
int MyValue = myrandom.Next();
string sFileName = mydocumentsPath + "\\tmp" + MyValue.ToString().Trim() + Extention;
byte[] filebyte;
CmdMain.CommandText = "Select LetterBody From VW_LetterSubject Where LetterCode=" + LetterCode;
filebyte = (byte[])CmdMain.ExecuteScalar();
FileStream FS = new FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write);
FS.Write(filebyte, 0, filebyte.Length);
FS.Flush();
FS.Close();
object FileName = sFileName;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(FileName, ReadOnly: false, Visible: true);
aDoc.Activate();
aDoc.Paragraphs.ReadingOrder = Microsoft.Office.Interop.Word.WdReadingOrder.wdRea dingOrderRtl;
FindAndReplace(wordApp, "#LetterNo#", this.txtLetterNo.Text.Trim());
FindAndReplace(wordApp, "#LetterDate#", this.farsiCalendarDateOfLetter.Value.FarsiSelected Date.ToString());
FindAndReplace(wordApp, "#RecipientOfTheLetter#", this.txtBankReceiver.Text.Trim());
FindAndReplace(wordApp,"#SubjectLetter#", this.txtContractSubject.Text);
FindAndReplace(wordApp,"#RequestNumber#", this.txtRequestNo.Text);
FindAndReplace(wordApp,"#Organ#", this.txtContractor.Text.Trim());
FindAndReplace(wordApp,"#ContractNo#", this.txtContractNo.Text.Trim());
FindAndReplace(wordApp,"#PriceContract#", this.txtPriceContract.Text.Trim());
FindAndReplace(wordApp,"#PriceGuarantee#", this.txtGuaranteePrice.Text.Trim());
FindAndReplace(wordApp,"#ContractDueDate#", this.txtContractDueDate.Text.Trim());
FindAndReplace(wordApp,"#NumberOfRenewals#", this.txtReNewNo.Text.Trim());
FindAndReplace(wordApp,"#GuaranteeNo#", this.txtGuarante_Cheque.Text.Trim());
FindAndReplace(wordApp, "#ContractSubject#", this.txtContractSubject.Text.Trim());
FindAndReplace(wordApp, "#ContracorName#", this.txtContractor.Text.Trim());
FindAndReplace(wordApp,"#AccountNumber#", this.raddgvData.SelectedRows[0].Cells[1].Value.ToString());
FindAndReplace(wordApp,"#BankName#", this.raddgvData.SelectedRows[0].Cells[0].Value.ToString());
ConData.Close();
}
else
{
foreach (string strFiles in System.IO.Directory.GetFiles(mydocumentsPath))
{
System.IO.File.Delete(strFiles);
}
Random myrandom = new Random();
int MyValue = myrandom.Next();
string sFileName = mydocumentsPath + "\\tmp" + MyValue.ToString().Trim() + Extention;
byte[] filebyte;
CmdMain.CommandText = "Select LetterBody From VW_LetterSubject Where LetterCode=" + LetterCode;
filebyte = (byte[])CmdMain.ExecuteScalar();
FileStream FS = new FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write);
FS.Write(filebyte, 0, filebyte.Length);
FS.Flush();
FS.Close();
object FileName = sFileName;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(FileName, ReadOnly: false, Visible: true);
aDoc.Activate();
aDoc.Paragraphs.ReadingOrder = Microsoft.Office.Interop.Word.WdReadingOrder.wdRea dingOrderRtl;
FindAndReplace(wordApp, "#LetterNo#", this.txtLetterNo.Text.Trim());
FindAndReplace(wordApp, "#LetterDate#", this.farsiCalendarDateOfLetter.Value.FarsiSelected Date.ToString());
FindAndReplace(wordApp, "#RecipientOfTheLetter#", this.txtBankReceiver.Text.Trim());
FindAndReplace(wordApp, "#SubjectLetter#", this.txtContractSubject.Text);
FindAndReplace(wordApp, "#RequestNumber#", this.txtRequestNo.Text);
FindAndReplace(wordApp, "#Organ#", this.txtContractor.Text.Trim());
FindAndReplace(wordApp, "#ContractNo#", this.txtContractNo.Text.Trim());
FindAndReplace(wordApp, "#PriceContract#", this.txtPriceContract.Text.Trim());
FindAndReplace(wordApp, "#PriceGuarantee#", this.txtGuaranteePrice.Text.Trim());
FindAndReplace(wordApp, "#ContractDueDate#", this.txtContractDueDate.Text.Trim());
FindAndReplace(wordApp, "#NumberOfRenewals#", this.txtReNewNo.Text.Trim());
FindAndReplace(wordApp, "#GuaranteeNo#", this.txtGuarante_Cheque.Text.Trim());
FindAndReplace(wordApp, "#ContractSubject#", this.txtContractSubject.Text.Trim());
FindAndReplace(wordApp, "#ContracorName#", this.txtContractor.Text.Trim());
FindAndReplace(wordApp, "#AccountNumber#", this.raddgvData.SelectedRows[0].Cells[1].Value.ToString());
FindAndReplace(wordApp, "#BankName#", this.raddgvData.SelectedRows[0].Cells[0].Value.ToString());
ConData.Close();
}


فقط یک مشکلی داره وقتی فایل ورد رو باز میکنه تاریخ رو برعکس جایگزین میکنه. با اینکه تو کد گفتم راست چین باشه ولی باز برعکس تاریخ رو نمایش میده.
تاریخ رو تو فایل ورد که جایگزین میکنه اینجوری هست: 15/07/1400
چجوری می تونم اینو حلش کنم؟