Link to home
Start Free TrialLog in
Avatar of cjJosephj
cjJosephj

asked on

Path issue

Hi,

I'm trying to save a file outside my root folder.

so instead of http://myserver/rootfolder/file.csv I would like to save the file to
http://myserver/c://filefolder/file.csv.

i'm using c sharp and was thinking of doing something like servername + '?c=' + HttpContent.Response. but not sure if this is correct.

Thanks in advance
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hi, that is easy, just change the code that save the file, for example, instead of using MapPath("") to get your application path, simply set your outside path manually, example:
This code will create a file inside the application path:
C#
System.IO.File.WriteAllText(MapPath("") + @"\MyFile.txt", "Hello!");

Open in new window

VB
System.IO.File.WriteAllText(MapPath("") & "\MyFile.txt", "Hello!")

Open in new window


This will create your file outside your root folder (C:\MyFolder):
C#
System.IO.File.WriteAllText(@"C:\MyFolder" + @"\MyFile.txt", "Hello!");

Open in new window

VB
System.IO.File.WriteAllText("C:\MyFolder" & "\MyFile.txt", "Hello!")

Open in new window


Maybe I misunderstood your question...
Avatar of cjJosephj
cjJosephj

ASKER

Thanks for your reply.


I'm already using this to create the file

  string Path = "C:" + "/" + filePath + fileName;
            using (StreamWriter sw = File.CreateText(Path))
            {
                sw.Write(contents);
                sw.Close();
            }

But I also need  to achieve something like

http://servername/c:/folder/file.csv
 
 which i  will  create using
      result.LoadXml("<root>" +
                   "<Url>" + servername + '?c=' +  filePath + fileName + "</Url>" + <--- not sure how to achive this line
             "</root>");
        }
        return result;

the reason for this is I need to create a csv file outside the root and then open the csv file in  my browser so i need the http to direct me to the file once it has been created on the C: drive.


Sorry for the confusion.
Ok! Now I understand what do you need, you need to setup a virtual directory in your IIS that point to the folder that contains yours csv files, you know what I meant? I'm from my cellphone right now, I will go to my pc to make you an example.
Thanks yu989c,

I have already created a virtual directory which contains my code. I would prefer to create the files on the c drive on the same server as at the moment the get created in the same folder as the folder that contains my code.

http://myserver/rootfolder(virtual directory)/file.csv
 but this is what I would like.
http://myserver/c://filefolder(folder on the c drive)/file.csv. <---- is this possible?

Thank you so much for your help so far.

Hi yu989c,

Please ignore my comment above and i will wait for your example.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, I’m almost there

I created the virtual directory but for some reason it does not create my file when I use the snippet below

 using (StreamWriter sw = File.CreateText(Server.MapPath("~") + "/" + filePath + fileName))
            {
                sw.Write(contents);
                sw.Close();
            }


This part works fine now

"<Url>" + servername + filePath + fileName + "</csv>" +

Thanks again
Hi buddy, if your file is outside of the application directory you dont need to make use of Server.MapPath("~") , try with this:
System.IO.File.WriteAllText(System.IO.Path.Combine(filePath, fileName), contents);

Open in new window

Please notice that filePath must be something like "C:\MyFolder\" and fileName something like "MyFile.csv"

By using the method System.IO.File.WriteAllText you dont need to use the StreamWriter implementation, just one line :)
Thank you yu989c :)

Glad to help!