关于Dictionary

fym_wlll 2016-08-27 09:07:38

现有一个 receivedFilesDict ,它的定义如下


Dictionary<ConnectionInfo, Dictionary<string, ReceivedFile>> receivedFilesDict = new Dictionary<ConnectionInfo, Dictionary<string, ReceivedFile>>();



其中,ReceivedFile类是这样的


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkCommsDotNet;
using System.ComponentModel;
using System.IO;

namespace student
{
/// <summary>
/// A local class which can be used to populate the WPF list box
/// </summary>
class ReceivedFile : INotifyPropertyChanged
{
/// <summary>
/// The name of the file
/// </summary>
public string Filename { get; private set; }

/// <summary>
/// The connectionInfo corresponding with the source
/// </summary>
public ConnectionInfo SourceInfo { get; private set; }

/// <summary>
/// The total size in bytes of the file
/// </summary>
public long SizeBytes { get; private set; }

/// <summary>
/// The total number of bytes received so far
/// </summary>
public long ReceivedBytes { get; private set; }

/// <summary>
/// Getter which returns the completion of this file, between 0 and 1
/// </summary>
public double CompletedPercent
{
get { return (double)ReceivedBytes / SizeBytes; }
set { throw new Exception("An attempt to modify readonly value."); }
}

/// <summary>
/// A formatted string of the SourceInfo
/// </summary>
public string SourceInfoStr
{
get { return "[" + SourceInfo.RemoteEndPoint.Address + ":" + SourceInfo.RemoteEndPoint.Port + "]"; }
}

/// <summary>
/// Returns true if the completed percent equals 1
/// </summary>
public bool IsCompleted
{
get { return ReceivedBytes == SizeBytes; }
}

/// <summary>
/// Private object used to ensure thread safety
/// </summary>
object SyncRoot = new object();

/// <summary>
/// A memorystream used to build the file
/// </summary>
Stream data;

/// <summary>
///Event subscribed to by GUI for updates
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Create a new ReceivedFile
/// </summary>
/// <param name="filename">Filename associated with this file</param>
/// <param name="sourceInfo">ConnectionInfo corresponding with the file source</param>
/// <param name="sizeBytes">The total size in bytes of this file</param>
public ReceivedFile(string filename, ConnectionInfo sourceInfo, long sizeBytes)
{
this.Filename = filename;
this.SourceInfo = sourceInfo;
this.SizeBytes = sizeBytes;

//We create a file on disk so that we can receive large files
data = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 8 * 1024, FileOptions.DeleteOnClose);
}

/// <summary>
/// Add data to file
/// </summary>
/// <param name="dataStart">Where to start writing this data to the interal memoryStream</param>
/// <param name="bufferStart">Where to start copying data from buffer</param>
/// <param name="bufferLength">The number of bytes to copy from buffer</param>
/// <param name="buffer">Buffer containing data to add</param>
public void AddData(long dataStart, int bufferStart, int bufferLength, byte[] buffer)
{
lock (SyncRoot)
{
data.Seek(dataStart, SeekOrigin.Begin);
data.Write(buffer, (int)bufferStart, (int)bufferLength);

ReceivedBytes += (int)(bufferLength - bufferStart);
}

NotifyPropertyChanged("CompletedPercent");
NotifyPropertyChanged("IsCompleted");
}

/// <summary>
/// Saves the completed file to the provided saveLocation
/// </summary>
/// <param name="saveLocation">Location to save file</param>
public void SaveFileToDisk(string saveLocation)
{
if (ReceivedBytes != SizeBytes)
throw new Exception("Attempted to save out file before data is complete.");

if (!File.Exists(Filename))
throw new Exception("The transfered file should have been created within the local application directory. Where has it gone?");

File.Delete(saveLocation);
File.Copy(Filename, saveLocation);
}

/// <summary>
/// Closes and releases any resources maintained by this file
/// </summary>
public void Close()
{
try
{
data.Dispose();
}
catch (Exception) { }

try
{
data.Close();
}
catch (Exception) { }
}

/// <summary>
/// Triggers a GUI update on a property change
/// </summary>
/// <param name="propertyName"></param>
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}




问题:
我想将receivedFilesDict里的所有内容的 Filename列出来,
如何写代码?下面的??处

[code=csharp]

foreach (Connection conn in NetworkComms.GetExistingConnection())
{

if (conn.ConnectionAlive())
{


MessageBox.Show(receivedFilesDict[conn.ConnectionInfo].Values.ToString() ); ??
//MessageBox.Show(conn.ConnectionInfo.RemoteEndPoint.ToString(), "查看连接数");
}


}


[/code]

...全文
87 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fym_wlll 2016-08-27
  • 打赏
  • 举报
回复
高手一出手,不知有没有,谢谢! 我也搞定了。

            foreach (Connection conn in NetworkComms.GetExistingConnection())
            {
                if (conn.ConnectionAlive())
                {
                    //------------------------------------------------------------
                    foreach (var ss in receivedFilesDict[conn.ConnectionInfo] )  //某连接的所有文件,一个一个的取来保存
                    {
                        MessageBox.Show(ss.Value.Filename.ToString() + "$" + ss.Value.IsCompleted.ToString() + "$" + ss.Value.SourceInfo.ToString());

                    }

                }


            }

Poopaye 2016-08-27
  • 打赏
  • 举报
回复
string.Join("\n", receivedFilesDict[conn.ConnectionInfo].Values.Select(x=>x.Filename).ToArray());

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧