小编Bro*_*ass的帖子

从流中读取数据的最有效方法

我有一个使用对称加密来加密和解密数据的算法.无论如何,当我要解密时,我有:

CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Read);
Run Code Online (Sandbox Code Playgroud)

我必须从cs CryptoStream中读取数据并将该数据放入一个字节数组中.所以一种方法可能是:

  System.Collections.Generic.List<byte> myListOfBytes = new System.Collections.Generic.List<byte>();

   while (true)
   {
                int nextByte = cs.ReadByte();
                if (nextByte == -1) break;
                myListOfBytes.Add((Byte)nextByte);
   }
   return myListOfBytes.ToArray();
Run Code Online (Sandbox Code Playgroud)

另一种技术可能是:

ArrayList chuncks = new ArrayList();

byte[] tempContainer = new byte[1048576];

int tempBytes = 0;
while (tempBytes < 1048576)
{
    tempBytes = cs.Read(tempContainer, 0, tempContainer.Length);
    //tempBytes is the number of bytes read from cs stream. those bytes are placed
    // on the tempContainer array

    chuncks.Add(tempContainer);

}

// later do …
Run Code Online (Sandbox Code Playgroud)

c# performance stream

25
推荐指数
2
解决办法
6万
查看次数

如何将Datetime字符串转换为当前文化日期时间字符串

我有英文字母"12/1/2011"在英国美国文化中我现在的机器文化是英国英国,这是"dd/mm/yyyy"格式.如何将12/1/2011转换为2011年1月12日.我试过以下格式.

System.DateTime.Parse(result,System.Threading.Thread.CurrentThread.CurrentCulture)
              .ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern)
Run Code Online (Sandbox Code Playgroud)

但我无法看到任何输出.

-Lokesh.

c#

22
推荐指数
4
解决办法
10万
查看次数

获取当前文件长度/ FileInfo.Length缓存和陈旧信息

我正在跟踪文件的文件夹及其文件长度,至少有一个文件仍在写入.

我必须不断更新每个文件长度的记录,我将其用于其他目的.

Update如果文件长度与上一次更新中确定的长度不同,则每15秒调用一次该方法并更新文件的属性.

update方法如下所示:

var directoryInfo = new DirectoryInfo(archiveFolder);
var archiveFiles = directoryInfo.GetFiles()
                                .OrderByDescending(f=>f.CreationTimeUtc); 
foreach (FileInfo fi in archiveFiles)
{
    //check if file existed in previous update already
    var origFileProps = cachedFiles.GetFileByName(fi.FullName);
    if (origFileProps != null && fi.Length == origFileProps.EndOffset)
    {
        //file length is unchanged
    }
    else
    {
        //Update the properties of this file
        //set EndOffset of the file to current file length
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道DirectoryInfo.GetFiles()预先填充许多FileInfo属性的事实包括Length- 只要更新之间没有缓存(缓存的信息不应超过15秒),这是可以的.

我假设每个DirectoryInfo.GetFiles()调用生成一个新 …

.net c# file-io

17
推荐指数
2
解决办法
4873
查看次数

Slick 3.1 - 检索列的子集作为案例类

我正在使用Slick 3.1.1,问题是在某些情况下我想省略一些相当重的列,并且仍然将列的子集实现为案例类.

请考虑以下表定义:

class AuditResultTable(tag: Tag) extends Table[AuditResult](tag, AuditResultTableName) {
    def auditResultId: Rep[Long] = column[Long]("AuditResultId", O.PrimaryKey, O.AutoInc)
    def processorId: Rep[Long] = column[Long]("ProcessorId")
    def dispatchedTimestamp: Rep[Timestamp] = column[Timestamp]("DispatchedTimestamp", O.SqlType("timestamp(2)"))
    def SystemAOutput: Rep[Array[Byte]] = column[Array[Byte]]("SystemAOutput", O.SqlType("LONGBLOB"))
    def SystemBOutput: Rep[Array[Byte]]  = column[Array[Byte]]("SystemBOutput", O.SqlType("LONGBLOB"))
    def isSuccessful: Rep[Boolean] = column[Boolean]("IsSuccessful")


def * : ProvenShape[AuditResult] = (processorId, dispatchedTimestamp, systemAOutput, systemBOutput, isSuccessful, auditResultId) <>
  (AuditResult.tupled, AuditResult.unapply) 

}  

val auditResults = TableQuery[AuditResultTable]  
Run Code Online (Sandbox Code Playgroud)

相应的案例类:

case class AuditResult (
   ProcessorId: Long,
   DispatchedTimestamp: Timestamp,
   SystemAOutput: Array[Byte],
   SystemBOutput: Array[Byte],
   IsSuccessful: Boolean,
   AuditResultId: …
Run Code Online (Sandbox Code Playgroud)

scala slick slick-3.0

17
推荐指数
1
解决办法
833
查看次数

使用WebSite项目在Visual Studio Dev Server上设置静态端口号

我在Visual Studio 2008中有一个WebSite项目(不是Web应用程序项目!)

如何将ASP.NET Development启动端口设置为WebSite项目中的静态端口?

configuration vs-web-site-project asp.net-development-serv visual-studio

13
推荐指数
1
解决办法
2万
查看次数

C#Linq返回SortedList

如何让Cq中的Linq返回SortedList给定的IEnumerable?如果我不能,是否有可能将其IEnumerable转换为SortedList

c# linq sortedlist

11
推荐指数
2
解决办法
6310
查看次数

LINQ:RemoveAll并删除元素

哪个是从列表中删除与某些条件匹配的项目的最简单方法,然后获取这些项目.

我可以从几个方面思考,我不知道哪个是最好的:

var subList = list.Where(x => x.Condition);
list.RemoveAll(x => x.Condition);
Run Code Online (Sandbox Code Playgroud)

要么

var subList = list.Where(x => x.Condition);
list.RemoveAll(x => subList.Contains(x));
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?如果是,哪一个?如果不是,我该怎么办?

c# linq removeall

10
推荐指数
3
解决办法
2万
查看次数

Hashtable碰撞重组 - 如何读取值?

我试图了解Hashtables如何在C#中工作.我阅读了MSDN文章,我理解C#Hashtables使用'rehashing'进行冲突,即如果我尝试将键/值对插入哈希表,如果使用HashFunction H1导致冲突,那么它将尝试HashFunction H2,H3等,直到找不到碰撞.

MSDN报价:

Hashtable类使用称为rehasing的不同技术.(有些消息来源称重新散列为双重散列.)

Rehashing的工作方式如下:有一组哈希不同的函数,H1 ... Hn,当从哈希表中插入或检索项时,最初使用H1哈希函数.如果这会导致碰撞,则尝试使用H2,如果需要,则转到Hn.上一节仅显示了一个哈希函数,即初始哈希函数(H1).其他散列函数与此函数非常相似,仅通过乘法因子进行区分.通常,散列函数Hk定义为:

Hk(key)= [GetHash(key)+ k*(1 +(((GetHash(key)>> 5)+ 1)%(hashsize - 1)))]%hashsize

但是,从MSDN site1获取示例:

private static Hashtable employees = new Hashtable();

public static void Main()
{
    // Add some values to the Hashtable, indexed by a string key
    employees.Add("111-22-3333", "Scott");
    employees.Add("222-33-4444", "Sam");
}
Run Code Online (Sandbox Code Playgroud)

假设添加第二个键将导致冲突,因此必须使用H2.但是,当我打电话给员工["222-33-4444"]时,哈希表如何知道使用H2?有没有单独的映射?谢谢.

c# hashtable

9
推荐指数
1
解决办法
4236
查看次数

asp.net c#membership:如何做一个GetUsersInRoles(多个角色)H

我想做一个"GetUsersInRoles",即我想找到所有MembershipUser在一组角色中至少有一个角色,但我似乎无法理解这个角色.

我有GetUsersInRole,Membership.GetAllUsers(),Linq,......但是怎么样?

任何反馈都非常感谢

汤米

c# linq membership asp.net roles

7
推荐指数
1
解决办法
8088
查看次数

具有依赖注入的单一类似行为

在对MEF进行一些研究后,我遇到了根据MSDN 的CreationPolicy.Shared属性:

指定CompositionContainer将创建关联ComposablePart的单个共享实例,并由所有请求者共享.

听起来不错,只要我始终确保只有一个容器访问我使用此策略导出的类.那么我该如何确保只有一个容器可以访问我的导出类型呢?这是我的场景:

我有一个Windows服务需要利用类似单一类的内存数据.数据是非持久性的,所以我希望每当服务启动时都会重新创建它,但是一旦服务停止它就没有用处.我的服务中的多个线程需要以线程安全的方式读取和写入此对象,因此我的初始计划是继承ConcurrentDictionary以确保针对它的线程安全操作.

将要攻击这个类的线程都是从一个抽象基类继承的,所以有没有办法让这个类(只有这个类)从MEF导入它并按照我想要的方式工作?

感谢您提供的任何提示,我对MEF很新,所以我还在学习细节

c# extensibility multithreading dependency-injection mef

7
推荐指数
1
解决办法
1066
查看次数