question

SathyanarayananAV-9576 avatar image
0 Votes"
SathyanarayananAV-9576 asked LanHuang-MSFT answered

error while debugging the project

'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";
 }
dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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://docs.microsoft.com/en-us/dotnet/api/system.io.path?view=net-6.0

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

DownloadFile (string Path)

As the error message clearly says, your variable "Path" is of type string and a string variable has no method GetFileName.
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @SathyanarayananAV-9576,
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.



image.png (27.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.