Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
i sheared a folder with EVERYONE permission and give permissions read and write and i tried to create new folder inside it by using the code below but i get error and the folder was creating in document/appdata ,inside the project files,what i do to solve it problem. thanks

What I have tried:

string targetPath = "‪\\\\MANAGER\\Archive Documents\\" + txtgetID.Text + " " + txtNumberB.Text + " " + txtDateB.Text;
if (!Directory.Exists("‪\\\\MANAGER\\Archive Documents\\" + Directory.CreateDirectory("‪\\\\MANAGER\\Archive Documents\\" + txtgetID.Text + " " + txtNumberB.Text + " " + txtDateB.Text)))
{
Directory.CreateDirectory("‪\\\\MANAGER\\Archive Documents\\" + Directory.CreateDirectory("‪\\\\MANAGER\\Archive Documents\\" + txtgetID.Text + " " + txtNumberB.Text + " " + txtDateB.Text));
}
else
{
foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
file.MoveTo(Path.Combine(targetPath, file.Name));
}
}
Posted
Updated 5-Feb-23 6:49am

First off, stop generating the directory name over and over and use the one string you started with.

Then it becomes more obvious what your problem is:
C#
if (!Directory.Exists("‪\\\\MANAGER\\Archive Documents\\" + Directory.CreateDirectory("‪\\\\MANAGER\\Archive Documents\\" + txtgetID.Text + " " + txtNumberB.Text + " " + txtDateB.Text)))
think about what director name you are checking.
Directory.CreateDirectory Method (System.IO) | Microsoft Learn[^] returns a DirectoryInfo struct, and the implicit ToString implementation returns the full path of the folder - which means the folder you are checking has two root elements:
C#
"\\\\MANAGER\\..." + "\\\\MANAGER\\..."

I think you should scrap that code and start again ...
 
Share this answer
 
What on earth is this?
C#
if(!Directory.Exists("‪\\\\MANAGER\\Archive Documents\\" + Directory.CreateDirectory("‪\\\\MANAGER\\Archive Documents\\" + txtgetID.Text + " " + txtNumberB.Text + " " + txtDateB.Text)))

Directory.Exists() returns a true/false value, which you cannot add to a .Text string with any meaning. Directory.CreateDirectory does not return a string but returns a DirectoryInfo object. So how does true + .Text + DirectoryInfo make ANY sense whatsoever, let alone a result that's evaluable in an if statement?

Also, your use of string concatenation makes debugging the code very difficult. Try interpolation instead.

And your else clause doesn't make sense. If the directory doesn't exist, you "create" it, but then nothing after that. The else clause will not get executed unless the directory already exists by the time the code gets to the if statement!
C#
string targetPath = $@"\\MANAGER\Archive Documents\{txtgetID.Text} {txtNumberB.Text} {txtDateB.Text}";
if (!Directory.Exists(targetPath))
{
    Directory.Create(targetPath);
}

foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
    string targetFilepath = Path.Combine(targetPath, file.Name);
    file.MoveTo(targetFilepath);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900