4

I have a strange problem. So my code follows as following.

  1. The exe takes some data from the user

  2. Call a web service to write(and create CSV for the data) the file at perticular network location(say \some-server\some-directory). Although this web service is hosted at the same location where this folder is (i.e i can also change it to be c:\some-directory). It then returns after writing the file

  3. the exe checks for the file to exists, if the file exists then further processing else quite with error.

The problem I am having is at step 3. When I try to read the file immediately after it has been written, I always get file not found exception(but the file there is present). I do not get this exception when I am debugging (because then I am putting a delay by debugging the code) or when Thread.Sleep(3000) before reading the file.

This is really strange because I close the StreamWriter before I return the call to exe. Now according to the documention, close should force the flush of the stream. This is also not related to the size of the file. Also I am not doing Async thread calls for writing and reading the file. They are running in same thread serially one after another(only writing is done by a web service and reading is done by exe. Still the call is serial)

I do not know, but it feels like there is some time difference between the file actually gets written on the disk and when you do Close(). However this baffling because this is not at all related to size. This happens for all file size. I have tried this with file with 10, 50, 100,200 lines of data.

Another thing which I suspected was since I was writing this file to a network location, it could be windows is optimizing the call by writing first to cache and then to network location. So I went ahead and changed the code to write it on drive(i.e use c:\some-directory), rather than network location. But it also resulted in same error.

There is no error in code(for reading and writing). As explained earlier, by putting a delay, it starts working fine. Some other useful information

  1. The exe is .Net Framework 3.5
  2. Windows Server 2008(64 bit, 4 GB Ram)

Edit 1 File.AppendAllText() is not correct solution, as it creates a new file, if it does not exits

Edit 2 code for writing

using (FileStream fs = new FileStream(outFileName, FileMode.Create))
        {
            using (StreamWriter writer = new StreamWriter(fs, Encoding.Unicode))
            {
                writer.WriteLine(someString)
            }
        }

code for reading

  StreamReader rdr = new StreamReader(File.OpenRead(CsvFilePath));
  string header = rdr.ReadLine();
  rdr.Close();

Edit 3 used textwriter, same error

  using (TextWriter writer = File.CreateText(outFileName))
    {
    }

Edit 3 Finally as suggested by some users, I am doing a check for the file in while loop for certain number of times before I throw the exception of file not found.

int i = 1;
while (i++ < 10)
{
   bool fileExists = File.Exists(CsvFilePath);
   if (!fileExists)
     System.Threading.Thread.Sleep(500);
   else
     break;
}
4

3 回答 3

1

因此,您正在将流写入文件,然后将文件读回流?您是否需要编写文件然后对其进行后期处理,或者您可以不直接使用源流吗?

如果您需要该文件,我将使用一个循环来不断检查文件是否存在,直到它出现(或经过很长时间) - 如果您无法写入文件,作者会给您一个错误,所以你知道它最终会出现。

于 2013-07-18T11:53:12.203 回答
0

你有没有尝试在处理完 writer FileStream 后阅读?

像这样:

using (FileStream fs = new FileStream(outFileName, FileMode.Create))
{
    using (StreamWriter writer = new StreamWriter(fs, Encoding.Unicode))
    {
        writer.WriteLine(someString)
    }
}

using (StreamReader rdr = new StreamReader(File.OpenRead(CsvFilePath)))
{
    string header = rdr.ReadLine();
}
于 2013-07-18T13:00:29.183 回答
0

由于您是通过网络编写的,因此最佳解决方案是首先将文件保存在本地系统中,然后将其复制到网络位置。这样可以避免网络连接问题。并且还有一个备份以防网络故障。

根据您的更新,试试这个:

File.WriteAllText(outFileName, someString);
header = null;
using(StreamReader reader = new StreamReader(CsvFilePath)) {
    header = reader.ReadLine();
}
于 2013-07-18T10:57:26.857 回答