I have a C# application (WCF soap service) that is creating PDF documents and save in in the path that is defined in web.config. Consider for example path is: C:\Doc\Pdf\ that in the code path is in location variable. I like to generate folders for each day and stores the pdfs for that day on its folder.
I tried to use CreateDirectory but I don't know how specify name of the folder to generate.
This code only save the PDF in the C:\Doc\Pdf\ and it is not creating any directory:
string pdfFileName = "Application" + "_" + documentData.APPLICATIONDATE.ToString("MMddyyyy") + "_" + documentData.APPLICATIONDATE.ToString("hhmmsstt") + "_" + documentData.BorrowerLastName;
location = ConfigurationManager.AppSettings["PdfPath"].ToString();
DirectoryInfo di =System.IO.Directory.CreateDirectory(location);
wordDoc.SaveAs(location + pdfFileName, WdSaveFormat.wdFormatPDF);
In this context I think that you could simply use the Directory.CreateDirectory method passing the combined path that you expect to be created.
The Directory method called CreateDirectory works creating all the directories missing in the path specified and if the path already exists doesn't throw exceptions, it simply does nothing
So your code coulde be
string dayPath = DateTime.Today.ToString("yyyyMMdd");
string newPath = Path.Combine(location, dayPath);
Directory.CreateDirectory(newPath);
........
Related
In my program I have to check, if a directory exists or not and when it doesn't the directory shall be created. I need that in two different classes. It works fine in the one, not at all in the other class. I could probably make this function a method and just call it in both places, but I was wondering how that can be.
So here's my code in class one:
string path = #"F:\yyyy\images\" + input+ "\\" + now + "\\";
if (!File.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
Where "now" is a string, containing the current date and input is just another string.
Code in class two:
string path = #"F:\yyyy\images\" + command[2] + "\\" + now+ "\\";
if (!File.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
The second example always runs, no matter if the directory exists or not.
command[2] is a string, basically being the same as "input" in the first example. To test if its really the same, I did this in class 2:
System.IO.Directory.CreateDirectory(path + 1);
And it made a "1" directory, inside the directory, that class 1 just created.
If you're trying to find check if a directory exists, use Directory.Exists instead of File.Exists. Your if check is unnecessary though. The Directory.Create method already does this check.
From the docs:
if (!File.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
You're checking to see if a file exists, and if it doesn't creating a directory with the name. At the end of this activity, there is still no directory that exists. What you want to do is see if the directory exists:
string path = #"F:\yyyy\images\" + input + "\\" + now;
if (!Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
Also: Notice that I got rid of the trailing backslash. It's not strictly necessary, and I tend to find it adds noise.
you want to check if the directory exists or the file?
Directory.Exists
File.Exists
I'm trying to create a file folder through the program to hold pdfs that I will later generate. I am doing so in my local and internal server, using the same code to do so:
if (!Directory.Exists(pathName))
Directory.CreateDirectory(pathName);
It works fine if it goes to local, but when I send it to the internal path the
Directory.CreateDirectory(pathNameInternal);
creates a file, not an actual file folder which is causing errors later in the code when it tries to access that named folder.
I have no idea why it is creating a file, anyone have any ideas?
Code from comment:
theOut = #"C:\inetpub\wwwroot\UserDownloads";
Directory.CreateDirectory(theOut + "\\" + DBName);
if (copyInternal == true)
{
theOutInternal = #"\\192.168.2.40\c$\inetpub\wwwroot\UserDownloads";
Directory.CreateDirectory(theOutInternal);
Directory.CreateDirectory(theOutInternal + "\\" + DBName);
}
I was wondering how I would go about doing this.
Normally I don't worry about this because I just stick the CSV file in the Debug Folder and reference it using:
filePath = Application.StartupPath + "\\blah.csv";
But I want do to this a bit differently now because I want to create a good baseline template project that contains this CSV file whenever I make a new project from the template.
But I still want the file path to be relative, IE) I don't want to have to edit the path to that CSV for each new project that I make.
Does anyone know how to set this up?
If I understand what you are needing you could go about it like this.
string appPath;
appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
appPath = appPath.Replace("file:\\", "");
appPath = appPath + #"\" + "filename.csv";
I am having a problem where I am trying to ZIP up a file using the below code :-
Process msinfo = new Process();
msinfo.StartInfo.FileName = "msinfo32.exe";
string path = "\"" + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + #"\test.nfo" + "\"";
string zippath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + #"\test.nfo";
MessageBox.Show(path);
msinfo.StartInfo.Arguments = #"/nfo "+path;
//msinfo.Start();
//msinfo.WaitForExit();
//MessageBox.Show("The File Has Been Saved!");
ZipFile.CreateFromDirectory(zippath, #"C:\Test.zip");
MessageBox.Show("Everything Is Done!");
The error that is coming is that the Folder path is not valid. I also tried by including quotation marks in the Zippath variable but it did not work.
PS - My machine name has 3 words so it has got spaces as well. Help is appreciated ^_^
The first argument of ZipFile.CreateFromDirectory should be a path of a directory, not a file (test.nfo in this case).
If you want compress the whole directory (e.g. the Desktop dir) then omit the "test.nfo" from the path, like this:
string zippath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
If you want to create a zip archive from only one file then use the ZipFileExtensions.CreateEntryFromFile.
One more thing: when you want to build a path from two or more components use the Path.Combine method instead of simple string concatenation. It can spare you from a lot of pain (like adding path separator characters).
I have an exception in the third line ofthis code "Second path fragment must not be a drive or UNC name"
DirectoryInfo labdi = new DirectoryInfo(Back.mainfolderpath + #"\news\l");
DirectoryInfo tld = new DirectoryInfo(labdi.FullName + #"\" + NorA.sn.labl[i]);
tld = labdi.CreateSubdirectory(labdi.FullName + #"\" + NorA.sn.labl[i] + #"\");
There is no useful way on the web.
Thank You.:!
I ran into this one today and finally tracked it down.
The exception is trying to tell you that when a DirectoryInfo takes a path as an argument (e.g., CreateSubdirectory or GetFiles), it will object if the path argument contains the Root and throw this elusive exception.
So path arguments that contain "C:\" or "D:\" etc do not work. Armed with this context, the exception message actually makes a bit of sense.
In your code, you were using the FullName property and this string contains "C:\" or whatever the root is.
Given the age of the question, I should add c#, .NET 4.5, VS2013.
The easiest solution to this problem is to use the static version of the Directory and File methods. You do not have to remove the root doing it this way. You also do not need the DirectoryInfo or FileInfo objects, they are what giving you headaches
string someFile = #"C:\somefolder\somefile.txt";
string directory = Path.GetDirectoryName(someFile);
foreach(var file in Directory.GetFiles(directory))
{
File.Delete(file);
}
The solution is to not put the full file path in the argument.
You already have the path of the parent directory as the object, so you only need to list the new directory name as an argument.
tld = labdi.CreateSubdirectory(NorA.sn.labl[i]);