create directory is not creating a file folder c# - c#

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);
}

Related

ASP.NET CORE, How read all files in a folder that is used from another process

im trying to read a folder from a remote machine, inside this folder there are many txt files.
The machine is writing every time new datas inside the folder.
string str = "";
try
{
DirectoryInfo d = new DirectoryInfo(#"\\192.168.1.209\user\HST\");
FileInfo[] Files = d.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
str = str + ", " + file.Name;
}
}
catch (Exception pp)
{
System.IO.File.WriteAllText(GlobalVariables.errorFolderLocation + "erroreLettura.1.209.txt", pp.ToString());
}
This is my code, I don't understand how to get these datas, because i get this error: "the system call level is not correct".
For example, if i try to delete the folder or a file, i get error because it's already used by another process.
So, is there a solution to "bypass" this error?
EDIT 1:
I need read every row of every file, i get the error on DirectoryInfo.
If i acces on the folders and files it works fine.
I need read this folder/files in 3 different machine, but only in this (192.168.1.209) not working, and its the only machine where i get error when i try to delete a file
Unfortunately, you are not able to delete a file which is in use by another process or delete a folder that contains a file in use, or whatever other operation that has not been specified to be accessed on the other process. In order to achieve this, the process which has the file in use, must open it with a share access, FileShare from System.IO .NET library.

Issue with deleting a binary file

I'm trying to delete a binary file from a directory in C#. But the File.Delete() method seems to work in misterious ways.
You see, when I try to delete the specified file from my directory, it doesn't work and the file is not deleted. However, when I open the directory where I have the file and then I execute my project to try to delete the file again, the file IS actually getting deleted and does not appear afterwards. What can be the issue?
This is my code:
currentDB.listTab = delTab.backup;
String fullPath = path + "\\" + delTab.delete;
File.Delete(fullPath);
txtStats.Text = "Deleted";
load_tables();
show_files();
I ask for the name of the file (delTab.delete) on a different window, and then I return the name in order to delete the file.

Include in setup excel file

i'm trying to include in my setup (pubish) an excel file, that is on my bin/debug folder.
When I click on properties, I have them as Build Action: Content and Copy to output directory: Copy always.
On my Application files I have:
my code looks like this:
string UDOFileName = System.AppDomain.CurrentDomain.BaseDirectory + "UDOs.xlsx";
if (System.IO.File.Exists(UDOFileName))
{
// Do some Work
}
else
{
MessageBox.Show("File not found");
}
When I run my program it works, but when I create the setup and run my application I get the "File not found" message.
What am I doing wrong?
St. Pat gave me the idea to switch to release mode and I changed the code to
string UDOFileName = System.AppDomain.CurrentDomain.BaseDirectory +
#"bin\Debug\UDOs.xlsx";
Now everything is working

How specify generated folder(Directory) Name in C#

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);
........

Directory.Move(): The process cannot access the file because it is being used by another process?

In my below code, I am trying to move all my .doc files located in one folder to another. In production, this will be moving files generated into created folder C:\Temp\ to a network folder that has a database job which every 5 minutes moves the files from the network folder into our document imagining archive system.
When trying my below test code, I receive "The process cannot access the file because it is being used by another process."
CODE:
public void moveLocalToCommitFYI()
{
// MOVE DOCS FORM TEMP FOLDER TO COMMITFYI FOLDER
string dirSource = #"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = #"C:\Users\NAME\Desktop\CommitTest\MoveTest\DestTest\";
try
{
Directory.Move(dirSource, dirDest);
}
catch (Exception ex)
{
MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
}
}
What appears strange to me is that when I specify a single source file outright and it's destination, code functions fine:
public void moveLocalToCommitFYI()
{
// MOVE DOCS FORM TEMP FOLDER TO COMMITFYI FOLDER
string dirSource = #"C:\Users\NAME\Desktop\CommitTest\MoveTestFile.doc";
string dirDest = #"C:\Users\NAME\Desktop\CommitTest\MoveTest\MoveTestFile.doc";
try
{
Directory.Move(dirSource, dirDest);
}
catch (Exception ex)
{
MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
}
}
Surely there is a way to move all my .doc files from the one directory to the other without needing to loop through and specify each individual file name?
EDIT:
Made the modification of having my destination folder be a different folder on my desktop instead of a sub-folder. dirSource contained 5 word documents and the destination directory (\MoveTest\ on my Desktop) contained no files:
string dirSource = #"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = #"C:\Users\NAME\Desktop\MoveTest\";
This generated "Cannot create a file when that file already exists".
Based on this, I assumed that the code is actually MOVING the set directory folder and all it's contents from one location to another, so I modified my code to the following:
string dirSource = #"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = #"C:\Users\NAME\Desktop\MoveTest2\";
This got rid of my sub-folder \MoveTest\ within \Desktop\CommitTest\ and created folder \Desktop\MoveTest2.
Is there a way for me to move the contents of the folder without getting rid of the source folder and place them into an already created destination?
Your test code is trying to move the entire directory into a directory inside itself. This is not a valid operation. Your actual code is probably trying to move the whole Temp directory to another location. This is not the same as moving all files inside it to another directory.
You'll have to loop through all file names and move each file individually.
var files = Directory.EnumerateFiles(dirSource, "*.doc")
.Select(path => new FileInfo(path));
foreach (var file in files)
file.MoveTo(Path.Combine(dirDest, file.Name));
If you find that the individual moves add too much overhead to the network traffic (especially important if this is a slow Internet connection and not a fast local connection), you might want to zip up all of your doc files (e.g. with ZipFile) before transmitting them to the server, and then have the server unzip them. This compression will probably lower the overall size, and transmitting one file instead of many will reduce the network overhead.
You cannot move a folder below itself. You need to find all its contents and move them. Or do this: rename MoveTest to MoveTest2, create a new MoveTest directory, and now you can move MoveTest under it.

Categories