ABA


"למה הוא ממשיך לזרוק לי פה הודעת שגיאה ?"
גירסת הדפסה        
קבוצות דיון פיתוח, תיכנות ובניית אתרים נושא #15803 מנהל    סגן המנהל    מפקח   Winner    צל"ש   מומחה  
אשכול מספר 15803
Nexus

   17:12   09.04.10   
אל הפורום  
  למה הוא ממשיך לזרוק לי פה הודעת שגיאה ?  
 
  

/*----------------------------------------------------------------
* Module Name : OptionsFile
* Description : Saves and retrievs application options
* Author : Danny
* Date : 10/02/2010
* Revision : 1.00
* --------------------------------------------------------------*/

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.Net;
using System.IO;
using AboutIt;
using General_utility;
using WindowsFormsApplication1;
using System.Configuration;


/*
* Introduction :
*
* This module helps in saving application options
*
*
* Typical file could look like this:
* user_color=Red
* time_left=30
*
*
*
*
*
* */

namespace DannyGeneral
{
class OptionsFile
{
/*----------------------------------------
* P R I V A T E V A R I A B L E S
* ---------------------------------------*/

/*---------------------------------
* P U B L I C M E T H O D S
* -------------------------------*/
string path_exe;
string temp_settings_file;
string temp_settings_dir;
string Options_File;
StreamWriter sw;
StreamReader sr;
FileStream fs;

/*----------------------------------------------------------
* Function : OptionsFile
* Description : Constructor
* Parameters : file_name is the name of the file to use
* Return : none
* --------------------------------------------------------*/
public OptionsFile(string settings )
{

if (File.Exists(settings))
{

}
else
{
File.Create(settings);
}
Options_File = settings;
path_exe = Path.GetDirectoryName(Application.ExecutablePath);


}

/*----------------------------------------------------------
* Function : GetKey
* Description : gets the value of the key.
* Parameters : key
* Return : value of the key if key exist, null if not exist
* --------------------------------------------------------*/
public string GetKey(string key)
{

// string value_of_each_key;
string key_of_each_line;
string line;
int index;
string key_value;
key_value = null;

sr=File.OpenText(Options_File);
while (null != (line = sr.ReadLine()))
{


index = line.IndexOf("=");


// value_of_each_key = line.Substring(index+1);

if (index >= 1)
{
key_of_each_line = line.Substring(0, index);
if (key_of_each_line == key)
{
key_value = line.Substring(key.Length + 1);
}

}
else
{
}

}
sr.Close();
return key_value;

}



/*----------------------------------------------------------
* Function : SetKey
* Description : sets a value to the specified key
* Parameters : key and a value
* Return : none
* --------------------------------------------------------*/
public void SetKey(string key , string value )
{

temp_settings_file = "\\temp_settings_file.txt";
temp_settings_dir = path_exe + @"\temp_settings";
Directory.CreateDirectory(temp_settings_dir);
File.Create(temp_settings_dir + temp_settings_file);
fs = new FileStream(temp_settings_dir + temp_settings_file, FileMode.Append);
sw = new StreamWriter(temp_settings_dir+temp_settings_file);
sw.WriteLine(key + "=" + value);
sw.Close();
return;

}

/*---------------------------------
* P R I V A T E M E T H O D S
* -------------------------------*/


}

}


                                שתף        
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

  האשכול     מחבר     תאריך כתיבה     מספר  
  בפונקציה של ה SETKEY : Nexus 09.04.10 17:15 1
  אם מה שאתה אומר זה נכון Sn00py  09.04.10 17:36 2

       
Nexus

   17:15   09.04.10   
אל הפורום  
  1. בפונקציה של ה SETKEY :  
בתגובה להודעה מספר 0
 
   כשהוא מגיע לשורה הזו

fs = new FileStream(temp_settings_dir + temp_settings_file, FileMode.Append);


הוא מחזיר לי שגיאה כותב Cant access file cuz file is being using by another process .

כאילו בגלל שאני יוצר את הקובץ קודם שורה לפני :

File.Create(temp_settings_dir + temp_settings_file);


אז הקובץ

temp_settings_file עדיין בשימוש .

אני לא מבין איך לפתור את זה מה אני לא עושה פה נכון .

תודה על העזרה .


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
Sn00py 
חבר מתאריך 1.8.02
2954 הודעות
   17:36   09.04.10   
אל הפורום  
  2. אם מה שאתה אומר זה נכון  
בתגובה להודעה מספר 0
 
   אז די מוזר, זה כאילו ה handle לקובץ לא נסגר, מה שמוזר, כי File היא מחלקה עם פונקציות סטטיות.. ה handle אמור להסגר ישר בסוף הפונקציה.
בכל מקרה, עדיף לך לכתוב לקובץ בצורה כזו:

path = temp_settings_dir + temp_settings_file
if (!File.Exists(path))
{
using (StreamWriter sw = File.Create(path))
{
sw.WriteLine(key + "=" + value);
}
}

זה יותר בטוח, ובסבירות גבוהה יפתור לך את הבעיה

\x6C\x65\x65\x74\x68\x61\x78\x30
\x72\x3A\x2D\x29
tresp4sser


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

תגובה מהירה  למכתב מספר: 
 
___________________________________________________________________

___________________________________________________________________
למנהלים:  נעל | תייק בארכיון | מחק | העבר לפורום אחר | מחק תגובות | עגן אשכול
       



© כל הזכויות שמורות ל-רוטר.נט בע"מ rotter.net