-1

The end-user supplies a path, indicating where the original document is.

string DocxFileName = "C:\\WorksArshad\\3.docx";

I'd like to create a copy of the document name 3.docx as 3Version1.docx and store the copy in the same directory as the original.

How do I get the whole path without the file name and extension?

(i.e.) I need to get the "C:\\WorksArshad\\" path alone.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Dah Sra
  • 4,107
  • 3
  • 30
  • 69
  • Use `File.Move("C:\\WorksArshad\\3.docx","C:\\WorksArshad\\test.docx")` – kostas ch. Jun 10 '14 at 13:18
  • @Grant : Its not only the matter of renaming ,i need to store the renamed file to the specified path where the original document is stored.-> – Dah Sra Jun 10 '14 at 13:19
  • @user3611781: then make a copy. Seems pretty straightforward. – siride Jun 10 '14 at 13:20
  • @siride: File.Copy("C:\WorksArshad\3", "C:\WorksArshad\3Version1.docx", true); its not possible because am getting the path from user (ie) File.Copy(fileName," ", true); – Dah Sra Jun 10 '14 at 13:25
  • FileInfo file = new FileInfo(C:\WorksArshad\3.docx); string path = file.Directory; In path i would get "C:\WorksArshad" This is what i need huh . .I lost my rep tooo.ANGRY – Dah Sra Jun 10 '14 at 13:32

1 Answers1

0
FileInfo file = new FileInfo(Session.FileName); 
string path = file.Directory.tostring();

and then using

        string fileName = Path.GetFileNameWithoutExtension(Session.FileName);
        string DocxFileNamee = path + "\\" + fileName + "V1.docx";
        File.Copy(Session.FileName, DocxFileNamee, true);

where in Session.FileName = "C:\WorksArshad\3.docx" and in path I'd get "C:\WorksArshad"

requirement solved .


Or

File.Copy(Session.FileName, Path.Combine(Path.GetDirectoryName(Session.FileName)
, Path.GetFileNameWithoutExtension(Session.FileName)+"V1.docx"),true);

both the above gives the solution

Dah Sra
  • 4,107
  • 3
  • 30
  • 69
  • i have edited your answer because it shows an exception and have updated the answer @GrantWinney – Dah Sra Jun 10 '14 at 14:11