使用c#进行文件传输的模拟 [英] simulation of file transferring using c#

查看:100
本文介绍了使用c#进行文件传输的模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我正在做一个使用c#.net通过蓝牙传输文件的项目,我成功完成了这个任务,现在我想证明使用c#在模拟传输中进行搜索,我搜索了很多但没有积极的结果. class ="h2_lin">解决方案

您可以使用进度条和复制剩余时间来显示模拟.

使用进度条进行简化非常容易.
1)只需放置一个进度条.
2)计算要复制的文件总数.
3)将此设置为进度条的最大属性.
4)在每个文件副本上使用委托来引用进度条,并也使用Application.DoEvents()来引用UI.

并且您必须共享您的代码如何使用蓝牙进行复制.但是我还没有尝试过.

我认为这可以解决所有问题.如果您有任何疑问,请随时问我.


如果您的问题只是模拟转移,那么就足够了:

 ///  <  摘要 > 
 /// 模拟文件传输.
 ///  <  /summary  > 
 ///  <  参数   名称="fileSize"  <  > 
 ///  <  参数   名称="bitsPerSecond"  <  > 
 无效传输( fileSize, float  bitsPerSecond)
{
     float  byteRate = bitsPerSecond/ 8 ;
    // 以秒为单位的时间
     float  timeNeeded = fileSize/byteRate;
    // 转换所需的时间(以毫秒为单位)
     int  ms =( int )(timeNeeded *  1000 );
    // 模拟传输时间
    System.Threading.Thread.Sleep(ms);
} 



如果您想显示进度,则可以使用:
ProgressForm:链接到BackgroundWorker的简单表单 [ 公共 静态 int CountFiles(字符串源, string 目标) { int fileCount = 0 ; 堆栈<文件夹> stack = Stack< Folder>(); stack.Push(文件夹(源,目标)); 同时(堆栈.计数> 0 ) { var path = stack.Pop(); foreach (字符串文件路径位于目录中. (path.sourcePath," )) { fileCount ++; } foreach (字符串 dir 位于目录中. (path.sourcePath)) { stack.Push(文件夹(目录,目标)); } } // 字符串[] files = Directory.GetFiles(source,"*.*",SearchOption.AllDirectories); // fileCount = files.Length; _fileCount = fileCount; 返回 fileCount; }



根据我的方法,我创建了一个类,并使用堆栈来计数和复制文件,这比递归技术要快.

然后仅从后台工作程序或使用相同的方法开始复制.

公共静态无效CopyFiles(ReportProgressWorker工作者,字符串源,字符串目标)
{
_avgTime =新的TimeSpan();
TimeSpan开始,结束;
string time = string.Empty;
堆栈<文件夹> stack =新的Stack< Folder>();
stack.Push(new Folder(source,destination));
copyedFile = worker;
while(stack.Count> 0)
{
var path = stack.Pop();
字符串dirpath = Path.Combine(path.destPath,Path.GetFileName(path.sourcePath));
if(!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
foreach(Directory.GetFiles(path.sourcePath,"*.*")中的字符串文件路径)
{
字符串targetpath = Path.Combine(dirpath,Path.GetFileName(filepath));
如果(File.Exists(targetpath))
File.Delete(targetpath);
开始= DateTime.Now.TimeOfDay;
File.Copy(filepath,targetpath);
_processedfiles ++;
结束= DateTime.Now.TimeOfDay;
字符串temp = ComputeTimeRequired(开始,结束);
如果(!string.IsNullOrEmpty(temp))
时间=温度;
copyedFile(Path.GetFileName(targetpath),time);
}
foreach(Directory.GetDirectories(path.sourcePath)中的字符串目录)
{
stack.Push(新文件夹(dir,Path.Combine(dirpath,Path.GetFileName(dir))));
}
}
}

此副本一起更新.

公共无效ReportTheProgress(字符串prg,字符串timeStatus)

使用此进度更改事件更新进度栏.我想它会做的一切.

如果我在任何地方都不对,请纠正我.
谢谢


Hi friends I''m doing a project in transferring files through bluetooth using c#.net and i finished that successfully and now i want to show that transferring in simulation using c# and i searched a lot but no positive result.

You can show simulation using a progress bar and time remaining to copy.

Simpulation using progess bar is very easy.
1) Just place a progess bar.
2) Count total number of files to be copied.
3) Set this as maximum property of progess bar.
4) Use a delegate to refersh progess bar on each file copy and use Application.DoEvents() too for refereshing UI.

And you must share your code how you copy using bluetooth. Yet I haven''t tried it.

I think this will do all. If you have any doubts please fell free to ask me.


If your problem is just simulating the transfer, then this should be enough:

/// <summary>
/// Simulates a file tranfer.
/// </summary>
/// <param name="fileSize">Size of the file.</param>
/// <param name="bitsPerSecond">Transfer speed.</param>
void Transfer(long fileSize, float bitsPerSecond)
{
    float byteRate = bitsPerSecond / 8;
    //time needed in seconds
    float timeNeeded = fileSize / byteRate;
    //convert the time needed in milliseconds
    int ms = (int)(timeNeeded * 1000);
    //simulate the transfer time
    System.Threading.Thread.Sleep(ms);
}



If you want something to show the progress, then you can use that:
ProgressForm: a simple form linked to a BackgroundWorker[^]


The simulation can go in such way.

Create a delete to referesh UI.
From you presentation layer start background worker.
In that call a method which actually identify location and count total number of files to be copied using below function.

By using this we can count files...

public static int CountFiles(string source,string destination)
       {
           int fileCount = 0;
           Stack<Folder> stack = new Stack<Folder>();
           stack.Push(new Folder(source, destination));
           while (stack.Count > 0)
           {
               var path = stack.Pop();
               foreach (string filepath in Directory.GetFiles(path.sourcePath, "*.*"))
               {
                   fileCount++;
               }
               foreach (string dir in Directory.GetDirectories(path.sourcePath))
               {
                   stack.Push(new Folder(dir, destination));
               }
           }
           //string [] files=Directory.GetFiles(source,"*.*",SearchOption.AllDirectories);
           //fileCount = files.Length;
           _fileCount = fileCount;
           return fileCount;
       }



According to my approch, I have created a class and used stack for counting and coying file which is more faster then recursive technique.

Then from Background worker only or from the same method start coying.

public static void CopyFiles(ReportProgressWorker worker,string source, string destination)
{
_avgTime = new TimeSpan();
TimeSpan start, end;
string time=string.Empty;
Stack<Folder> stack = new Stack<Folder>();
stack.Push(new Folder(source, destination));
copiedFile = worker;
while (stack.Count > 0)
{
var path=stack.Pop();
string dirpath=Path.Combine(path.destPath,Path.GetFileName(path.sourcePath));
if(!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
foreach (string filepath in Directory.GetFiles(path.sourcePath,"*.*"))
{
string targetpath=Path.Combine(dirpath,Path.GetFileName(filepath));
if (File.Exists(targetpath))
File.Delete(targetpath);
start = DateTime.Now.TimeOfDay;
File.Copy(filepath, targetpath);
_processedfiles++;
end = DateTime.Now.TimeOfDay;
string temp = ComputeTimeRequired(start, end);
if (!string.IsNullOrEmpty(temp))
time = temp;
copiedFile(Path.GetFileName(targetpath),time);
}
foreach (string dir in Directory.GetDirectories(path.sourcePath))
{
stack.Push(new Folder(dir, Path.Combine(dirpath, Path.GetFileName(dir))));
}
}
}

This copy and update together.

public void ReportTheProgress(string prg,string timeStatus)

using this progess change event update progess bar. I think it will do all.

If I am wrong anywhere please correct me guys.
Thanks


这篇关于使用c#进行文件传输的模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆