并非所有代码路径都返回值C# [英] Not all code paths returns a value C#

查看:148
本文介绍了并非所有代码路径都返回值C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经返回下面的上传文件代码并将文件移动到我编写了单独的函数的文件夹,需要调用它们。但我收到的错误是并非所有代码路径都返回值请告诉我哪里出错了。



我尝试了什么:



Hi, i have return below code for upload files and move file to a folder i have written separate functions and need to call them. but i am getting the error as "NOT ALL CODE PATHS RETURNS A VALUE" please suggest where i went wrong.

What I have tried:

 public void BtnUploadClick(object sender, EventArgs e)
		{
		 	try{
            int CheckBoxRowCounter = 0;            
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
                {
                    CheckBoxRowCounter++;
                }
            }
             if (CheckBoxRowCounter > 0) 
             {
                  //  String folderPath = folderBrowserDlg.SelectedPath;
                    for (int i = 0; i < dataGridView1.RowCount; i++)
                    {
                        if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
                        {
                            string SourceFilePath = dataGridView1.Rows[i].Cells[1].Value.ToString();
                            string FileName = new System.IO.FileInfo(SourceFilePath).Name;
                             Encrypt_and_decrypt.AES a  = new AES();                            
                            string TargetFilePath = _SelectedPath + @"\" + FileName;    
                             FileInfo file1=new FileInfo(SourceFilePath);		
                             a.EncryptFile(SourceFilePath,"D:\\test"+file1.Extension.ToString(),Hidkey[0,1].ToString());                             
                           //upload                       
                                        				                       
                            //Move upload                                    
				                 
                       }
                    }
                    MessageBox.Show("Selected Files Moved.Refreshing Grid...");
                    FillGridView();
             }
					 else
            {
                MessageBox.Show("Please select item(s).");
            }
					 //return upload(SourceFile;destinationfile);
			}
			catch( Exception e3)
			{
				MessageBox.Show(e3.Message);
			}
      	}  

		 public int upload(string SourceFile,string destinationfile)
		 {
		 	try{
		 	       		using (WebClient client = new WebClient())
		             	  {
			          		  client.Credentials = new NetworkCredential("username", "password");     
							  client.UploadFile("ftpaddress" + SourceFile, destinationfile);			          		  
		                	}
                           File.Delete(destinationfile);   
		 	}
		 
		 	catch(Exception e)
		 	{
		 		MessageBox.Show(e.Message);
		 	}
		 }
		 public void move(string sourcefile,string destfile)
		 {
		 	string subFolder = Path.Combine(_SelectedPath, "UPLOAD");
		        if (!Directory.Exists(subFolder))				
			      {				
		              Directory.CreateDirectory(subFolder);				
	              }
	               String Todaysdate = DateTime.Now.ToString("MMM-dd-yyyy");
	               string datefolder = Path.Combine(subFolder,Todaysdate);
			      if(!Directory.Exists(datefolder))
		          {
			    	Directory.CreateDirectory(datefolder);
			      }
		            string sourcePath = _SelectedPath;
			        string targetPath = datefolder;
				    string sourceFile = System.IO.Path.Combine(sourcePath, FileName);
			        string destFile = System.IO.Path.Combine(targetPath, FileName);						                 
				    //destFile = System.IO.Path.Combine(targetPath, FileName);
				     System.IO.File.Move(sourceFile, destFile);			                   
		 }
  }
}

推荐答案

上传方法的返回类型为int但它是没有返回任何值。
The upload method has a return type of int but it's not returning any value.


尝试这样的事情:



Try something like this:

public int upload(string SourceFile,string destinationfile)
        {
           try{
                       using (WebClient client = new WebClient())
                         {
                             client.Credentials = new NetworkCredential("username", "password");
                             client.UploadFile("ftpaddress" + SourceFile, destinationfile);
                           }
                          File.Delete(destinationfile);
                          return 1;
           }

           catch(Exception e)
           {
               MessageBox.Show(e.Message);
                               return 0;
           }





所以两个退出点都有回报。



so both exit points have a return.


这篇关于并非所有代码路径都返回值C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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