在asp.net中怎么创建以及读写一个.ini文件 ?在创建完把数据写到这个ini文件后怎么把他设成只读文件
最好给个可以用的源代码 不要发连接,小弟属于比较笨的那种,看不懂
最好给个可以用的源代码 不要发连接,小弟属于比较笨的那种,看不懂
解决方案 »
- 利用资源文件做中英文双版网站
- 浏览器提示框
- aspx转换成html
- 文件上传<input type=file....>必须吗,怎么用脚本改变里面的值
- 请问如何为网页生成图片
- 谁知道哪个控件能实现这样的功能啊?
- 一个棘手的问题,将异常alert出来时候遇到的白屏问题。
- 我下载了一个用treeview控件做的目录树,里面有个iewebcontrols.msi文件,是用来做什么的?
- |G|die
- [高手请进,关于WEB 2.0的问题]
- DateGrid 的能分两行显示嘛?? 就事20列,一行显示10列 ??高手执教~!~
- 在asp.net中怎么创建以及读写一个.ini文件 ? 发了好几个帖子了,只要有人解决,这几帖的分都给他绝不失言
System.IO.FileInfo fi = new System.IO.FileInfo(strFile);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
{
fi.Attributes = System.IO.FileAttributes.Normal;
}
ini就按纯文本读就行
mikecatfile.create("c:\mikecat.ini")open方法
function open(filename,filemode,fileaccess) as filestream
该方法返回的是文件操作通道对象filestream。
filemode和fileaccess在system.io名字空间里各自定义了一组枚举常量,用于指定文件操作模式和操作权限。
filemode.append 以追加的方式打开文件,或者以追加的方式创建一个新的文件。使用这种模式操作文件时,必须和fileaccess.write一起使用,就是说必须有写入权限
filemode.create 创建一个新文件,如果存在同名的文件,将覆盖原文件
filemode.createnew 创建一个新文件,如果有同名文件,打开文件出错
filemode.open 打开一个已经存在的文件
filemode.openorcreate 打开一个已经存在文件,如果该文件不存在则创建一个新文件。
filemode.truncate 当文件打开时清空文件的所有内容,如果使用这个属性对文件至少要有写入的权限 fileaccess.read 打开的文件只有读取的权限
fileaccess.write 打开的文件只有写入的权限
fileaccess.readwrite 打开的文件既可以写入也可以读取
http://blog.csdn.net/cncxz/archive/2005/08/23/462755.aspx
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;namespace INIManage
{
public class INIManage
{ [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionsNamesA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int GetPrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
[DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileSectionsA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int WritePrivateProfileSections(string lpAppName, string lpString, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
#endregion
private string _Filename; //INI文件名
private string _Sections; //INI文件中配置参数的组别片段
private const int MAX_ENTRY = 32768; //最大字符数
public INIManage(string strFile)
{
this.Filename = strFile;
} #region" INI操作类的属性 " public string Filename
{
get
{
return this._Filename;
}
set
{
this._Filename = value;
}
}
public string Sections
{
get
{
return this._Sections;
}
set
{
this._Sections = value;
}
}
#endregion
#region" INI操作类的Read相关方法 "
// Read相关方法中的 DefaultValue是 在INI文件中找不到相关配置 时的返回值
//ReadBoolean是读取bool类型的配置参数,ReadByteArray是读取 byte[]类型的配置参数
//ReadInteger是读取int类型的配置参数。依次类推 public bool ReadBoolean(string Key)
{
return this.ReadBoolean(this.Sections, Key);
} public bool ReadBoolean(string Key, bool DefaultValue)
{
return this.ReadBoolean(this.Sections, Key, DefaultValue);
} public bool ReadBoolean(string Sections, string Key)
{
return this.ReadBoolean(Sections, Key, false);
} public bool ReadBoolean(string Sections, string Key, bool DefaultValue)
{
return bool.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
} public byte[] ReadByteArray(string Key, int Length)
{
return this.ReadByteArray(this.Sections, Key, Length);
} public byte[] ReadByteArray(string Sections, string Key, int Length)
{
byte[] buffer1;
if (Length > 0)
{
try
{
byte[] buffer2 = new byte[(Length - 1) + 1];
if (INIManage.GetPrivateProfileStruct(Sections, Key, buffer2, buffer2.Length, this.Filename) == 0)
{
return null;
}
return buffer2;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
buffer1 = null;
ProjectData.ClearProjectError();
return buffer1;
}
}
else
{
return null;
}
}
public int ReadInteger(string Key)
{
return this.ReadInteger(Key, 0);
} public int ReadInteger(string Key, int DefaultValue)
{
return this.ReadInteger(this.Sections, Key, DefaultValue);
} public int ReadInteger(string Sections, string Key)
{
return this.ReadInteger(Sections, Key, 0);
} public int ReadInteger(string Sections, string Key, int DefaultValue)
{
int num1;
try
{
num1 = INIManage.GetPrivateProfileInt(Sections, Key, DefaultValue, this.Filename);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
num1 = DefaultValue;
ProjectData.ClearProjectError();
return num1;
}
return num1;
}
{
return this.ReadLong(Key, (long) 0);
} public long ReadLong(string Key, long DefaultValue)
{
return this.ReadLong(this.Sections, Key, DefaultValue);
} public long ReadLong(string Sections, string Key)
{
return this.ReadLong(Sections, Key, 0);
} public long ReadLong(string Sections, string Key, long DefaultValue)
{
return long.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
} public string ReadString(string Key)
{
return this.ReadString(this.Sections, Key);
} public string ReadString(string Sections, string Key)
{
return this.ReadString(Sections, Key, "");
} public string ReadString(string Sections, string Key, string DefaultValue)
{
string text1;
try
{
StringBuilder builder1 = new StringBuilder(MAX_ENTRY);
int num1 = INIManage.GetPrivateProfileString(Sections, Key, DefaultValue, builder1, MAX_ENTRY, this.Filename);
text1 = builder1.ToString();
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
text1 = DefaultValue;
ProjectData.ClearProjectError();
return text1;
}
return text1;
} #endregion
#region" INI操作类的Write相关方法 "
public bool Write(string Key, bool Value)
{
return this.Write(this.Sections, Key, Value);
} public bool Write(string Key, byte[] Value)
{
return this.Write(this.Sections, Key, Value);
} public bool Write(string Key, string Value)
{
return this.Write(this.Sections, Key, Value);
} public bool Write(string Key, int Value)
{
return this.Write(this.Sections, Key, Value);
} public bool Write(string Key, long Value)
{
return this.Write(this.Sections, Key, Value);
} public bool Write(string Sections, string Key, byte[] Value)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileStruct(Sections, Key, Value, Value.Length, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} public bool Write(string Sections, string Key, bool Value)
{
return this.Write(Sections, Key, Value.ToString());
} public bool Write(string Sections, string Key, int Value)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value.ToString(), this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} public bool Write(string Sections, string Key, long Value)
{
return this.Write(Sections, Key, Value.ToString());
} public bool Write(string Sections, string Key, string Value)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} #endregion
#region" INI操作类的Delete相关方法 " public bool DeleteKey(string Key)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(this.Sections, Key, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} public bool DeleteKey(string Section, string Key)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} public bool DeleteSections(string Section)
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileSections(Sections, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
} #endregion
public ArrayList GetSectionsNames()
{
int num1;
ArrayList list1 = new ArrayList();
byte[] buffer1 = new byte[MAX_ENTRY];
int num2 = 0;
try
{
num1 = INIManage.GetPrivateProfileSectionsNames(buffer1, MAX_ENTRY, this.Filename);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
return list1;
}
ASCIIEncoding encoding1 = new ASCIIEncoding();
if (num1 > 0)
{
string text1 = encoding1.GetString(buffer1);
num1 = 0;
num2 = -1;
while (true)
{
num1 = text1.IndexOf('\0', (int) (num2 + 1));
if (((num1 - num2) == 1) || (num1 == -1))
{
return list1;
}
try
{
list1.Add(text1.Substring(num2 + 1, num1 - num2));
}
catch (Exception exception2)
{
ProjectData.SetProjectError(exception2);
ProjectData.ClearProjectError();
}
num2 = num1;
}
}
return list1;
}
}
}
FileStream fs = null;using(fs = new FileStream(filePath, FileAccess.Create))
{
fs.Write("[section1]\n");
fs.Write("key1=value1\n");
fs.Write("key2=value2\n");
fs.Close();
}File.SetAttributes(filePath, FileAttributes.ReadOnly);