C# 为什么filestream返回的长度为0?

C# 为什么filestream返回的长度为0?,c#,.net,.net-4.5,visual-studio-2015,C#,.net,.net 4.5,Visual Studio 2015,所以我一直在这里遇到溢出异常,我发现这是错误的计算。但是当我设置一个断点时,代码中的fs.Length-startAt的结果等于startAt的精确倒数,但是在watch窗口中,同样的结果计算为我所期望的数字 这是怎么回事?我重新启动了visualstudio;没有变化 private async Task DoTail() { await Task.Run(async () => { while (!_cts.IsCancellationRequeste

所以我一直在这里遇到溢出异常,我发现这是错误的计算。但是当我设置一个断点时,代码中的
fs.Length-startAt
的结果等于
startAt
的精确倒数,但是在watch窗口中,同样的结果计算为我所期望的数字

这是怎么回事?我重新启动了visualstudio;没有变化

private async Task DoTail()
{
    await Task.Run(async () =>
    {
        while (!_cts.IsCancellationRequested)
        {
            await UpdateFile();
            await Task.Delay(_interval);
        }
    }, _cts.Token);

    Debug.WriteLine($"Stopped tailing {LogInfo.Alias}");
}

private async Task UpdateFile()
{
    try
    {
        if (!File.Exists(LogInfo.Location))
        {
            Trace.WriteLine($"{LogInfo.Location} was not found.");
            Contents = $"File not found: {LogInfo.Location}";
            _cts.Cancel();
            return;
        }

        using (var fs = new FileStream(LogInfo.Location, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            if (!fs.CanRead || fs.Length == _lastIndex) return; // no change

            // avoid reading the entire file on startup
            long startAt = _lastIndex;
            if (startAt == 0 && fs.Length > _displayBuffer)
            {
                startAt = fs.Length - _displayBuffer;
                Trace.WriteLine($"File was larger than buffer ({_displayBuffer}). Starting at i={startAt} instead of beginning.");
            }

            long contentLength = fs.Length - startAt;
            if (contentLength < 0)
            {
                // trying to track some weird overflow case
                Trace.WriteLine($"!!!!!! Length: {fs.Length} startAt: {startAt}");
                return;
            }

            var newContent = new byte[contentLength];
            Trace.WriteLine($"This chunk will be {newContent.Length} bytes.");

....
}
private async Task DoTail()
{
等待任务。运行(异步()=>
{
而(!\u cts.iscancellationrequest)
{
等待更新文件();
等待任务。延迟(_间隔);
}
},_cts.Token);
Debug.WriteLine($“已停止跟踪{LogInfo.Alias}”);
}
私有异步任务更新文件()
{
尝试
{
如果(!File.Exists(LogInfo.Location))
{
Trace.WriteLine($“{LogInfo.Location}未找到。”);
Contents=$“未找到文件:{LogInfo.Location}”;
_cts.Cancel();
返回;
}
使用(var fs=new FileStream(LogInfo.Location,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
如果(!fs.CanRead | | fs.Length==_lastIndex)返回;//无更改
//避免在启动时读取整个文件
long startAt=_lastIndex;
if(startAt==0&&fs.Length>\u displayBuffer)
{
startAt=fs.Length-\u显示缓冲区;
Trace.WriteLine($”文件大于缓冲区({u displayBuffer})。从i={startAt}开始,而不是从“.”)开始;
}
long contentLength=fs.Length-startAt;
if(contentLength<0)
{
//试图追踪一些奇怪的溢出案例
Trace.WriteLine($“!!!!!!!长度:{fs.Length}startAt:{startAt}”);
返回;
}
var newContent=新字节[contentLength];
Trace.WriteLine($“此块将为{newContent.Length}字节。”);
....
}

我会说它溢出是因为一个奇怪的原因,请尝试
fs.Lenght
startAt
的较小值,以查看是否仍然得到那个奇数负数。它溢出是因为它导致我用一个负值初始化那个字节数组。对于低至33和12的数字,我遇到了这个问题。它将ate 33-12=-12,但仅在代码中。监视窗口对其进行了良好的评估。我明白了。您将鼠标悬停在
var contentLength
的开始处。这是一个IDE,尽管@JoelCoehoorn,OP,您可以在另一台PC上重新编程吗?您是否签入了发布或调试模式?在发布模式下,一些变量已优化,因此您无法获得正确的re苏丹