error while debugging the project

SathyanarayananAV 1 Reputation point
2022-09-13T09:25:04.993+00:00

'String' does not contain a definition for GetFileName and no accessible extension method 'GetFileName' accepting a first argument of type 'string' could be found/are you missing a using directive or an assembly reference. I am getting error in GetFileName

[WebMethod]  
public static string DownloadFile(string Path)  
{  
  string str1 = Uri.UnescapeDataString(Path);  
  string fileName = Path.GetFileName(str1);  
  string str2 = "Result : " + DateTime.Now.ToString() + " - From Server";  
  FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath("~/UploadFile/") + fileName);  
  HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";  
  HttpContext.Current.Response.AppendHeader("Content-Disposition", "Attachment; Filename=" + fileName);  
  HttpContext.Current.Response.WriteFile(str1);  
  HttpContext.Current.Response.End();  
  return "true";  
}  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 26,186 Reputation points
    2022-09-13T10:43:47.057+00:00

    The problem is you named the DownloadFile() method's input parameter "Path". Use the fully qualified name to get the file name.

    System.IO.Path.GetFileName(str1);  
    

    https://learn.microsoft.com/en-us/dotnet/api/system.io.path?view=net-6.0

    0 comments No comments

  2. Olaf Helper 40,656 Reputation points
    2022-09-13T11:00:39.657+00:00

    DownloadFile (string Path)

    As the error message clearly says, your variable "Path" is of type string and a string variable has no method GetFileName.

    0 comments No comments

  3. Lan Huang-MSFT 25,386 Reputation points Microsoft Vendor
    2022-09-14T05:39:29.487+00:00

    Hi @SathyanarayananAV ,
    You just need to change the string Path to string path. The string you defined conflicts with the method name.
    240846-image.png

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments