문제

I am getting " Could not find a part of the path" error while copying a file from server to local machine. here is my code sample:

 try
            {
                string serverfile = @"E:\installer.msi";
                string localFile = Path.GetTempPath();
                FileInfo fileInfo = new FileInfo(serverfile);
                fileInfo.CopyTo(localFile);
                return true;
            }
            catch (Exception ex)
            {
                return false;

            }

Can anyone tell me what's wrong with my code.

도움이 되었습니까?

해결책

Path.GetTempPath

is returning you folder path. you need to specify file path as well. You can do it like this

string tempPath = Path.GetTempPath();
string serverfile = @"E:\installer.msi";
string path = Path.Combine(tempPath, Path.GetFileName(serverfile));
File.Copy(serverfile, path); //you can use the overload to specify do you want to overwrite or not

다른 팁

You should copy file to file, not file to directory:

...
  string serverfile = @"E:\installer.msi";
  string localFile = Path.GetTempPath();
  FileInfo fileInfo = new FileInfo(serverfile);

  // Copy to localFile (which is TempPath) + installer.msi
  fileInfo.CopyTo(Path.Combine(localFile, Path.GetFileName(serverfile)));
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top