Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行此代码时不会创建文件_C#_Winforms_File - Fatal编程技术网

C# 运行此代码时不会创建文件

C# 运行此代码时不会创建文件,c#,winforms,file,C#,Winforms,File,我有以下代码: List<String> suma = new List<String>(); if (File.Exists(Application.StartupPath + "/totalsold" + username)) suma = new List<String>(File.ReadAllLines(Application.StartupPath + "/totalsold" + username)); List<String&g

我有以下代码:

List<String> suma = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsold" + username))
     suma = new List<String>(File.ReadAllLines(Application.StartupPath + "/totalsold" + username));
List<String> actual = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsold" + username))
   actual = new List<String>(File.ReadAllLines(Application.StartupPath + "/soldproducts" + username));
List<String> sumatotal = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsoldadmin"))
    sumatotal = new List<String>(File.ReadAllLines(Application.StartupPath + "/totalsoldadmin"));      
 StreamWriter vanzare = new StreamWriter(Application.StartupPath + "/soldproducts" + username);
 StreamWriter total = new StreamWriter(Application.StartupPath + "/totalsold" + username);
 StreamWriter totall = new StreamWriter(Application.StartupPath + "/totalsoldadmin");   

问题解决了

是否关闭该文件?对文件的写入可能不会立即发生,因为.NET和操作系统可能都在缓存,从而延迟写入。但是,当您打开
StreamWriter
时,该文件应立即显示

对于文件的短期使用(例如,写东西然后关闭文件),您一定要使用
using
语句:

using (StreamWriter vanzare = new StreamWriter(...)) {
   vanzare.WriteLine("Hello World");
}
这将确保随后立即正确关闭文件,并且不会将任何非托管资源保留超过所需时间


如果需要将文件保持打开状态的时间超过单个方法的时间,那么当然必须手动执行此操作。确保当您不再需要StreamWriter(以及其他
IDisposable
s)时,可以对其调用
Dispose()
方法。

您在文件路径中使用的斜杠可能不起作用,具体取决于您使用的平台

使用
Path.Combine
方法连接路径和文件名,它将使用适合文件系统的路径分隔符:

string sold = Path.Combine(Application.StartupPath, "totalsold" + username);
if (File.Exists(sold)) {
  suma = new List<String>(File.ReadAllLines(sold));
}
string sell=Path.Combine(Application.StartupPath,“totalsell”+用户名);
如果(文件存在(已出售)){
suma=新列表(File.ReadAllLines(已售出));
}

不仅要关闭它,还要刷新它。PedroC88,如果在
StreamWriter上调用
Close()
Dipose()
,就会发生这种情况。他们的代码看起来根本不像(缺少使用
语句的
),好像他们知道可以正确地关闭和处置对象。。。你说得对。我想我现在会修改我自己的代码XD。我按照你说的做了,但它没有做任何更改!既然你接受了答案,有什么变化?我得到的印象是这没用。(但你的问题也没有)在执行IO操作时,总是养成使用(){}块的习惯。。。如果这不是一个要求,为什么不检查
环境.SpecialFolder
枚举()。由于IO的关键性,使用()块是操作IO最安全的方法。因此,此块确保用于这些IO的资源被正确释放。我使用了using()块,但它不起作用!!您是否在任何地方捕获异常?这是您正在运行的实际代码吗?这不是问题所在,问题是没有创建vanzare、total、total文件,也没有在其中写入内容。@EmilDumbazu:您确定没有创建这些文件吗?如果路径分隔符错误,它们可能会被创建在与预期不同的文件夹中。但是,斜杠在Windows上不会造成任何问题(至少在Win32 API级别上不会出现问题。使用
cmd
的内置项则是另一回事)。(当然,这不是不使用
路径的借口。Combine
,同意这一点;))我确信!运行应用程序时,我一直在查看不同的文件夹。
string sold = Path.Combine(Application.StartupPath, "totalsold" + username);
if (File.Exists(sold)) {
  suma = new List<String>(File.ReadAllLines(sold));
}