我尝试使用代码编辑文本文件的一行。我在Windows 7上的visual studio .Net 4.5上使用了c#。
这是我的代码:
public static void Updateconfigfile(bool updateall, int linenotoedit)
{
if (updateall == true)
{
//update all lines of files
MessageBox.Show("start 1");
} else
{
MessageBox.Show("start");
noprojecton = 10;
//update only a specified line
//line index for file.readalllines beguins at zero
string updatestring = Updateconfigfilegetvalue(1);
string[] textfile = File.ReadAllLines(appdataconfigpath);
MessageBox.Show(textfile[0]);
textfile[0] = Updateconfigfilegetvalue(0);
MessageBox.Show(textfile[0]);
File.WriteAllLines(appdataconfigpath, textfile);
}
}
public static string Updateconfigfilegetvalue(int lineno)
{
//determine which line the user wants to update starts at line 0
//return the correct line
string toreturn = "";
switch(lineno)
{
case 0:
toreturn = ("numberofprojectson: " + noprojecton);
return toreturn;
default:
return "";
}
}
在文件中,当前的一行文字表示' numberofprojectson:1' 当我运行它时,我显示了说开始的消息。
然后有一条消息说' 1'然后一个人说' 10'
所以它以字符串数组形式获取文本文件并更改数组的0索引,但它不会将其写入文本文件。由于文本文件仍然表示' numberofprojectson:1'。
我检查了文件路径,并使用断点检查所有代码正在执行,并且它正在执行每一行并写入正确的文件。
任何帮助都将不胜感激。
控制台输出:
'
'Pseudocode Writer.exe' (CLR v4.0.30319: DefaultDomain): Loaded C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'E:\Pseudocode Writer\Pseudocode Writer\bin\Debug\Pseudocode Writer.exe'. Symbols loaded.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Pseudocode Writer.exe' (CLR v4.0.30319: Pseudocode Writer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'System.IO.IOException' in mscorlib.dll
The program '[4276] Pseudocode Writer.exe' has exited with code 0 (0x0).
应用数据配置路径
public static String appdatapathprovisonal = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
appdatapath = (appdatapathprovisonal + asciicharbackslash + "Pseudocode Writer");
appdataconfigpath = (appdatapath + asciicharbackslash + "config.pwc");
public static string asciicharbackslash = (Convert.ToChar(92)).ToString();
92 ascii代码是' \' .pwc是一个文本文件(.txt)
在控制台中抛出异常:
Exception thrown: 'System.IO.IOException' in mscorlib.dll
答案 0 :(得分:0)
感谢所有帮助。事实证明,当文件已被另一个进程使用时,会发生此异常。在我的情况下,这是因为我忘了关闭流阅读器。因此,如果您遇到此错误,请记住检查文件是否在程序中以其他方式使用。再次感谢!