[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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
0
cjJosephj
Asked:
cjJosephj
  • 5
  • 5
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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...
0
 
cjJosephjAuthor Commented:
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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
cjJosephjAuthor Commented:
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.

0
 
cjJosephjAuthor Commented:
Hi yu989c,

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

Thanks
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi cjJosephj, a virtual directory is not necessarily a subdirectory in your web application, it can point to any folder in your file system, also, this dont work if your are running your web application by using the ASPNET Development Server, you need to publish your application over IIS to make use of virtual directories.

Example:
By using virtual directories you can point a url to a specific folder in your file system, in this way:
Your local folder: C:\MyFolder ---> http://myserver/MyFolder
Or
Your local folder: C:\MyFolder ---> http://myserver/MyFiles/MyFolder
Or
Your local folder: D:\Data\OtherFolder ---> http://myserver/Data

I meant, you can play with the url that point to a specific folder in your system.


So I dont know what IIS version are you using (IIS 5-6, IIS7??), here you have a guides to do that:

Here you will find a how to Create and Configure Virtual Directories (IIS5-6):
http://msdn.microsoft.com/en-us/library/zwk103ab.aspx

Over IIS 7:
http://msdn.microsoft.com/en-us/library/bb763173.aspx

Please feel free to write if you need more help about this.
0
 
cjJosephjAuthor Commented:
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
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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 :)
0
 
cjJosephjAuthor Commented:
Thank you yu989c :)

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to help!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now