如何只计算C#中的加密文件 [英] How to count only encrypted files in C#

查看:92
本文介绍了如何只计算C#中的加密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试下面的代码来获取加密文件的数量,但是未加密的文件也是我得到的数量,请建议我哪里出错./或需要更新。我正在数据网格视图中选择文件。如果我选​​择四个文件,只有3个文件正在加密并保存在文件夹中,但我计算为4个文件已加密。



我尝试过:



Hi,

I am trying below code to get the count of the encrypted files but the file which was not encrypted also i am getting as a count, please suggest where i went wrong./ or need to update. i am selecting files in the data gridview .if i select four files only 3 files are encrypting and saving in the folder, but i am getting count as 4files encrypted.

What I have tried:

if (CheckBoxRowCounter > 0) {	
					int i;
					string TargetFilePath = "";	
				
					for ( i = 0; i < dataGridView1.RowCount; i++) {
						try {								
							if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value)) {
								string SourceFilePath = dataGridView1.Rows[i].Cells[1].Value.ToString();
								FileName = new System.IO.FileInfo(SourceFilePath).Name;								
								Encryption.AES a = new AES();
								TargetFilePath = subFolder + @"\" + FileName;
								FileInfo file1 = new FileInfo(SourceFilePath);		
								a.EncryptFile(SourceFilePath, TargetFilePath, Hidkey[0, 1].ToString());
								btnopenEncfolder.Enabled=true;
								//File.Delete(SourceFilePath);
							}
						} catch (Exception e2) {  
							MessageBox.Show(e2.Message);
						}
					} 
					if(FileName.Length>0)
					{
				//	System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(SelectedPath);
					int count = 0;
					for (i=0;i<dataGridView1.Rows.Count;i++){
						if(Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
						{							
							count++;
						}
					}						
					MessageBox.Show(count +"file sucessfully encrypted");
					return;
					}
					if (FileName.Length==0)
					{
                     MessageBox.Show("file encryption failed"+  (FileName));
					}				
//					  System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(SelectedPath);
//					  int count = dir.GetFiles().Length;
//					   MessageBox.Show(count+"files encrypted");
					  
					FillGridView();	

推荐答案

只需在执行加密时递增计数,加密成功:

Just increment the count when you perform the encryption respectively encrypting was successful:
int selectedForEncrypt = 0;
int encrypted = 0;
for ( i = 0; i < dataGridView1.RowCount; i++) {
    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value)) {
        try {								
            selectedForEncrypt++;
            // Do the encryption here
            // ...

            // Encryption was successful when no exception occured
            // Otherwise this will not get executed
            encrypted++;
        }
        catch (Exception e2) {  
            MessageBox.Show(e2.Message);
        } 
    }
} 
MessageBox.Show(encrypted + " file(s) sucessfully encrypted");


你是什么意思如果我选择四个文件只有3个文件正在加密和保存该文件夹,但我被计为4个文件加密。



如果你试图加密一个文件少于网格中的总文件,那么问题在于 for loop

what do you mean by "if i select four files only 3 files are encrypting and saving in the folder, but i am getting count as 4 files encrypted."

If you are trying to encrypt one file less than total files in grid then the problem is with the for loop
for (i=0;i<dataGridView1.Rows.Count;i++){





你是用i = 0开始的,直到 i< dataGridView1.Rows.Count 即,如果您有4行,则计数为4,并且您正在执行i = 0到i = 3(4次)的操作。尝试



you are starting it with i=0 and doing it till i<dataGridView1.Rows.Count ie, if you have 4 rows the count is 4 and you are doing the operation for i = 0 to i=3 (4 times). try

for (i=1;i<dataGridView1.Rows.Count;i++){







循环需要什么?






What is the need for this for loop

int count = 0;
for (i=0;i<dataGridView1.Rows.Count;i++){
    if(Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
    {
        count++;
    }
}







如果你想带算你可以在第一个for循环内执行它




if you want to take count you can do it inside the first for loop itself

int count = 0
for ( i = 0; i < dataGridView1.RowCount; i++) {
						try {								
							if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value)) {
								string SourceFilePath = dataGridView1.Rows[i].Cells[1].Value.ToString();
								FileName = new System.IO.FileInfo(SourceFilePath).Name;								
								Encryption.AES a = new AES();
								TargetFilePath = subFolder + @"\" + FileName;
								FileInfo file1 = new FileInfo(SourceFilePath);		
								a.EncryptFile(SourceFilePath, TargetFilePath, Hidkey[0, 1].ToString());
								btnopenEncfolder.Enabled=true;

 count++;
								//File.Delete(SourceFilePath);
							}
						} catch (Exception e2) {  
							MessageBox.Show(e2.Message);
						}
					} 


这篇关于如何只计算C#中的加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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