none
Problem to line

    Question

  • Hi,
    How to resolve
    [DirectoryNotFoundException: Could not find a part of the path '~/empty.jpg'.]
       System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +338
       System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) +550
       Own_rec._Default.Page_Load(Object sender, EventArgs e) +580
          
    due to this line

    System.IO.File.Copy("~/empty.jpg", str0, true);

    Many Thanks & Best Regards, Hua Min


    Friday, March 31, 2017 7:45 AM

Answers

  • Hi HuaMin Chen,

    This is Visual C# forum. As your issue is related to Web, please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off-topic.

    https://forums.asp.net/

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by HuaMin Chen Friday, April 7, 2017 3:20 AM
    Thursday, April 6, 2017 1:08 PM
    Moderator

All replies

  • feels like you need to provide the correct/full source path. 

    Check here


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, March 31, 2017 9:28 AM
  • Try this:

       System.IO.File.Copy(Server.MapPath("~/empty.jpg"), str0, true);

    See also: https://forums.asp.net/

    Friday, March 31, 2017 9:31 AM
  • feels like you need to provide the correct/full source path. 

    Check here


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Hi,

    The source and destination file name are valid.


    Many Thanks & Best Regards, Hua Min

    Friday, March 31, 2017 9:51 AM
  • Hi,

    Presumably this is related to ASP.NET - that being the case you need to use Server.MapPath to determine the actual path to the file.

    Also File.Copy won't create the destination directory for you, so you need to make sure that it exists, and that it's path is also correct. Finally, you need to make sure that the user you are operating under has appropriate rights to see everything.

    Regards,

    D

    Friday, March 31, 2017 11:26 AM
  • Hi,

    Can we still use File.Copy on Code behind of Web page?


    Many Thanks & Best Regards, Hua Min

    Friday, March 31, 2017 2:35 PM
  • Hi,

    Yes, but the problem you will have is that the hosting process is operating as a specific user. That user needs to have sufficient access rights to the area you are trying to write to or you'll get an exception.

    Regards,

    D

    PS don't forget to accept an answer

    Friday, March 31, 2017 2:37 PM
  • But I actually was to copy the file and NOT to create any folder, while the error is NOTHING related to creating folder, right?

    Many Thanks & Best Regards, Hua Min

    Friday, March 31, 2017 3:44 PM
  • Hi,

    That doesn't seem right - File.Copy works on explicit paths, not relative paths. To get this to work, you need to translate between the paths that your web server understands to the actual paths on the hosting server - this mapping needs to take place for both the source file AND the destination file. i.e. you need to call MapPath on both parameters to the function.

    Regards,

    D

    Saturday, April 1, 2017 6:38 AM
  • If I did put such line
    System.IO.File.Copy("C:/inetpub/VS2012/Own_rec4/empty.jpg", str0, true);

    I do not know why the file still cannot be copied as expected, while str0 is having value like

    C:/inetpub/VS2012/Own_rec4/output63626911810308.jpg 



    Many Thanks & Best Regards, Hua Min


    • Edited by HuaMin Chen Thursday, April 6, 2017 7:09 AM
    Thursday, April 6, 2017 7:08 AM
  • Hi,

    Assuming you have the access rights correctly set up, so that the user under which IIS is running has write privileges to the folder specified, I can't see why that wouldn't work.

    Can you post more of your code?

    Regards,

    D

    Thursday, April 6, 2017 12:53 PM
  • Hi HuaMin Chen,

    This is Visual C# forum. As your issue is related to Web, please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off-topic.

    https://forums.asp.net/

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by HuaMin Chen Friday, April 7, 2017 3:20 AM
    Thursday, April 6, 2017 1:08 PM
    Moderator