I would like to accomplish the following tasks in a C#.net 2010 application:
1. start in a specified directory location.
2. check to see if a specified subfolder exists.
3. if the specified folder does not exist, create the subfolder called customer1.
4. if the subfolder called 'docs' does not exist, create that folder.
5. if the type of document type is '*.pdf' or '*.doc' place the data file in the subfolder called
'docs'.
if the document type is of any other document type, place that document in folder level where called 'customer1'.
Thus I am wondering if you can tell me and/or point me to references that will show me how to accomplish all and/or parts of the steps I would like to accomplish listed above my showing me showing me code and/or pointing me to links that will show me how to accomplish this goal?
Loading
Akkiraju IvaturiPosted Aug 29, 2012, 7:59 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FolderTest
{
class Program
{
static void Main(string[] args)
{
// 1. start in a specified directory location.
//Directory location : C:\TestFolder
String firstFolder = @"C:\TestFolder";
if (!Directory.Exists(firstFolder))
{
Directory.CreateDirectory(firstFolder);
}
//2. check to see if a specified subfolder exists.
//3. if the specified folder does not exist, create the subfolder called customer1.
string subFolder = Path.Combine(firstFolder, "Customer1");
if (!Directory.Exists(subFolder))
{
Directory.CreateDirectory(subFolder);
}
//4. if the subfolder called 'docs' does not exist, create that folder.
string subFolder1 = Path.Combine(firstFolder,"Docs");
if (!Directory.Exists(subFolder1))
{
Directory.CreateDirectory(subFolder1);
}
// 5. if the type of document type is '*.pdf' or '*.doc' place the data file in the subfolder called
//'docs'.
// if the document type is of any other document type, place that document in folder level where called 'customer1'.
string fileName = @"C:\abc.txt";
FileInfo fileInfo = new FileInfo(fileName);
string justFileName = Path.GetFileName(fileName);
if ((fileInfo.Extension == ".pdf") || (fileInfo.Extension == ".doc"))
{
if (justFileName != null) File.Copy(fileName, Path.Combine(subFolder1, justFileName));
}
else
{
if (justFileName != null) File.Copy(fileName, Path.Combine(subFolder, justFileName));
}
}
}
}
dcPosted Aug 29, 2012, 5:04 PM
However the code to copy a file from one location to another does not work.
The lines I am referring to are:
File.Copy(fileName, Path.Combine(subFolder1, fileName));
File.Copy(subFolder,justFileName);
Thus can you tell me what to do differently so I can copy the files from one location to another location?
Akkiraju IvaturiPosted Aug 28, 2012, 9:57 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FolderTest
{
class Program
{
static void Main(string[] args)
{
// 1. start in a specified directory location.
//Directory location : C:\TestFolder
String firstFolder = @"C:\TestFolder";
if (!Directory.Exists(firstFolder))
{
Directory.CreateDirectory(firstFolder);
}
//2. check to see if a specified subfolder exists.
//3. if the specified folder does not exist, create the subfolder called customer1.
string subFolder = Path.Combine(firstFolder, "Customer1");
if (!Directory.Exists(subFolder))
{
Directory.CreateDirectory(subFolder);
}
//4. if the subfolder called 'docs' does not exist, create that folder.
string subFolder1 = Path.Combine(firstFolder,"Docs");
if (!Directory.Exists(subFolder1))
{
Directory.CreateDirectory(subFolder1);
}
// 5. if the type of document type is '*.pdf' or '*.doc' place the data file in the subfolder called
//'docs'.
// if the document type is of any other document type, place that document in folder level where called 'customer1'.
string fileName = @"C:\abc.txt";
FileInfo fileInfo = new FileInfo(fileName);
string justFileName = Path.GetFileName(fileName);
if ((fileInfo.Extension == ".pdf") || (fileInfo.Extension == ".doc"))
{
File.Copy(fileName, Path.Combine(subFolder1, fileName));
}
else
{
if (justFileName != null) File.Copy(subFolder,justFileName);
}
}
}
}