2016-04-26 37 views
-2

我正在創建一個基本的MVC複製文件應用程序,並返回一個不是所有代碼路徑在我的GetSource方法中返回一個值錯誤。我已經研究過這個問題並嘗試了一些東西,但是仍然會產生錯誤。我相信這個錯誤是在foreach循環的某個地方,但我試着返回一個null,這只是拋出了一個不同的錯誤。任何幫助表示讚賞,因爲我是初學者。謝謝!MVC,C# - 並非所有的代碼路徑返回值

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.IO; 


namespace April24 
{ 
    public class Andrew 
    { 
     public string Copy() 
     { 
      return "Done Copying"; 
     } 
     public string GetSource() //Error is here 
     { 
      copyFiles(10); 
     } 
     public static string production = @"C:\Users\test\Desktop\Production"; 
     public static string renameFolder = @"C:\Users\test\Desktop\RenameFolder\"; 

     static private void copyFiles(int numberOfFiles) 
     { 
      List<string> files = System.IO.Directory.GetFiles(production, "*").ToList(); 
      IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_Name")).Take(10); 

      foreach (string file in filesToCopy) 
      { 

       string destfile = renameFolder + System.IO.Path.GetFileName(file); 

       System.IO.File.Copy(file, destfile, true); 
      }; 
      /*tried return null; here but still threw error*/ 

     } 
    } 
} 
+1

你想要什麼'GetSource'返回? –

+4

因爲你不在'GetSource()'中返回任何東西(你已經指定它需要返回'string')。你'copyFiles()'是'void'(它不會返回任何東西) –

+0

謝謝丹尼爾。它應該將文件從'製作'複製到'重命名文件夾'中。此代碼作爲控制檯應用程序工作,但在將其複製到MVC框架時拋出錯誤。 – AndrewC10

回答

3

你的方法簽名說的getSource返回一個字符串,而函數體沒有返回值,這就是編譯器抱怨

public string GetSource() //Error is here 
{ 
     copyFiles(10); 
     return "A string here" 
} 
相關問題