path logic problem c#

  • Thread starter Thread starter LuisIngles
  • Start date Start date
L

LuisIngles

Guest
Well im building a program in console c# that asks your path and filename, but i cant make it to take the files from my specified path how can i solve this? it only executes files from the app/debug directory.


public class Main1
{
string respuesta = string.Empty;
public string filePath = " ";
bool directoryFound = true;


public string FilesLoad()
{
do
{
if (!directoryFound)
{
Console.WriteLine("This directory not found:\n{0}\nPlease double check file path. \nPlease Double check that this program has read and write access to the directory.\n", filePath);
}

Console.WriteLine("\n");
Console.WriteLine("Please specify file location:");
filePath = @Console.ReadLine();

directoryFound = Directory.Exists(filePath);
if (filePath.Length >= 240)
{
directoryFound = false;
Console.WriteLine("Please keep the filepath under 240 chars so that you still are able to provide a name for the file.");
}
} while (!directoryFound);
foreach(string s in Directory.GetFiles(filePath).Select(Path.GetFileName))
Console.WriteLine(s);
return filePath = GetFileName(filePath);

}

public static string GetFileName(string filePath)
{
string fileName = " ";
bool isValidName = true;
int fileNameMaxLength = 0;
do
{
if (!isValidName)
{

Console.WriteLine("{0} \nis not a valid filename. Make sure you enter a valid filename.\nYour max filename length should be {1}", fileName, fileNameMaxLength);
}

Console.WriteLine("\n");
Console.WriteLine("Please enter the name of the file:");
fileName = Console.ReadLine();

string illegalChars = @"^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\"";|/]+$";

isValidName = Regex.IsMatch(fileName, illegalChars, RegexOptions.CultureInvariant);

if (fileName.Length + filePath.Length > 254)
{
isValidName = false;
fileNameMaxLength = 254 - filePath.Length;
}

} while (!isValidName);

// Get the data from path.
string sampleCSV = fileName;

string[,] values = LoadCSV(sampleCSV);
int num_rows = values.GetUpperBound(0) + 1;
int num_cols = values.GetUpperBound(1) + 1;

// Display the data to show we have it.

for (int c = 0; c < num_cols; c++)
Console.Write(values[0, c] + "\t");

//Read the data.
for (int r = 1; r < num_rows; r++)
{
// dgvValues.Rows.Add();
Console.WriteLine();
for (int c = 0; c < num_cols; c++)
{
Console.Write(values[r, c] + "\t");
}
}

Console.WriteLine("Done");
Console.Read();
return Path.Combine(filePath, (fileName + ".xlsx"));

}

public static string[,] LoadCSV(string filename)
{
// Get the file's text.
string whole_file = System.IO.File.ReadAllText(filename);

// Split into lines.
whole_file = whole_file.Replace('\n', '\r');
string[] lines = whole_file.Split(new char[] { '\r' },
StringSplitOptions.RemoveEmptyEntries);

// See how many rows and columns there are.
int num_rows = lines.Length;
int num_cols = lines[0].Split(',').Length;

// Allocate the data array.
string[,] values = new string[num_rows, num_cols];

// Load the array.
for (int r = 0; r < num_rows; r++)
{
string[] line_r = lines[r].Split(',');
for (int c = 0; c < num_cols; c++)
{
values[r, c] = line_r[c];
}
}

// Return the values.
return values;
}


}

Continue reading...
 
Back
Top