-3

I have created a wpf application in that I have the requirement to create a folder and in that folder I want a text file to be created dynamically in the application installed folder

C:\Program Files (x86)\MyApplication\NewFolder\Mytext.txt\

like wise.

I've tried the following code but it is not getting

using System.IO;

private void CreateIfMissing(string path)
{
  bool folderExists = Directory.Exists(Server.MapPath(path));//Here i don't understand what is server i think this will work in ASP.NET
  if (!folderExists)
  Directory.CreateDirectory(Server.MapPath(path));
}

How to acheive this?

user3702363
  • 11
  • 1
  • 2
  • Google is your friend: http://stackoverflow.com/a/9065650/1387161 – Bjorn Vdkerckhove Jun 03 '14 at 14:20
  • 1
    [File.Create](http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx). Doesn't this help? – Rohit Vats Jun 03 '14 at 14:21
  • This is extremely simple and trivial to solve if you'd just make an attempt to look for the answer. Can you show what you've tried? It sounds like you're just asking us to do it for you. – tnw Jun 03 '14 at 14:21
  • It's first thing in your Google search if you type in "c# create file" – PiotrWolkowski Jun 03 '14 at 14:22
  • Why are you using Server.MapPath if you have tagged this as `WPF`? – Peter Ritchie Jun 03 '14 at 14:25
  • If the code is running on the local machine you don't need to reference the remote path. It should create on your local drive, in which case you can just put the full path in a string. Be sure to parameterize your string and put @ or \\ if you don't use the @. – apollosoftware.org Jun 03 '14 at 14:28

1 Answers1

1

First you import the IO library for C# / .NET

using System.IO;

Check if it exists, if not create it with the following.

 if (!Directory.Exists(@"C:\Program Files (x86)\MyApplication\NewFolder")
       Directory.CreateDirectory(@"C:\Program Files (x86)\MyApplication\NewFolder");

In all honesty, if you did some research, buddy you'd find it! Good luck with your project

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Hi actually i've tried the code you have given but the i'm getting the error saying access is denied for that folder it's perfectly working for other paths except what i want. – user3702363 Jun 03 '14 at 14:37
  • using System.IO; /** * @returns A StreamWriter if no Exception occured or a null object if anything occured */ private StreamWriter CreatIfMissing(string path) { StreamWriter writer = null; String newFolderPath = path + @"\NewFolder"; if(!Directory.Exists(newFolderPath)) { try { Directory.CreateDirectory(newFolderPath); } catch{} } String newFilePath = newFolderPath + @"\MyText.txt"; try { writer = File.CreateText(newFilePath); } catch{} return writer; } – Hybris95 Jun 03 '14 at 15:10
  • The account you are using is crapping out. It's your NTFS permissions for that directory. Make sure the user account you are using has create/modify permissions. Delegate an admin account, and run the .NET code as that account that is capable of manipulating the NT file System. – apollosoftware.org Jun 03 '14 at 15:15
  • On Windows Vista and later, that folder (Program Files) is protected. You can't create files in that folder. You should be using the predefined folder for AppData: `var folder = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApplication");` – Chris Dunaway Jun 03 '14 at 15:20