I am currently writing a text editor program, however I have run into a problem.
I am attempting to allow the user to open a file with my program by double-clicking it, which is achieved by setting the default program. I have been told that this sends the program the filepath as a command argument.
I am using it like this:
private void formMain_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
string filePath = args[1];
addTab();
getFontCollection();
setFontSizes();
getCurrentDocument.Text = (File.ReadAllText(filePath));
}
However, I consistently get the following error:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
Additional information: The given path's format is not supported.
If someone would please direct me to fixing this, it would be greatly appreciated.
By the way, the entire source code is located on Github, github.com/Criticaldiamonds/asys
EDIT
According to MSDN, the first argument is the program itself, followed by user-specified arguments. Therefore,
args[0] = the program
args[1] = "C:\users\Giovanni\Desktop\Hello.txt" (w/o quotes ofc)
Because the VS debugger escapes characters, the value of args[1] in the debugger is "C:\\users\\Giovanni\\Desktop\\Hello.txt"
You should use the Methods on the System.IO.Path Class
private void Form1_Load(object sender, EventArgs e)
{
var args = System.Environment.GetCommandLineArgs();
// using linq here reduces that array count check then extract
var argPath = args.Skip(1).FirstOrDefault();
if (!string.IsNullOrEmpty(argPath))
{
// Your .LoadFile(...) method requires a full path
var fullPath = Path.GetFullPath(argPath);
/* this part isn't needed unless you want to ensure the directory exists... but if it doesn't exist you can't open it anyway
var dirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
*/
/* this isn't needed since you are using the full path
Directory.SetCurrentDirectory(dirPath);
*/
addTab();
getFontCollection();
setFontSizes();
getCurrentDocument.LoadFile(fullPath, RichTextBoxStreamType.PlainText);
}
}
I managed to solve the problem! Thank you to everyone who left comments, they really did help!
By messing around with directory settings, I ended up with the following code, which correctly loads the test file!
private void formMain_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
string dirPath = args[1];
string fileName = "";
fileName = Path.GetFileName(dirPath);
dirPath = dirPath.Substring(3);
dirPath = Path.GetFullPath(dirPath);
if (dirPath.Contains('\\')) dirPath = dirPath.Substring(0, dirPath.LastIndexOf('\\'));
Directory.SetCurrentDirectory(dirPath);
addTab();
getFontCollection();
setFontSizes();
getCurrentDocument.LoadFile(dirPath + '\\' + fileName, RichTextBoxStreamType.PlainText);
}
Related
I am using Directory.CreateDirectory(string) method to create folders, now the problem is if the user enters string as:
"C:\folder1" then it creates the folder in the respective location, fine by me.
but if he writes
"C:\\\\\\\\\\\\\\\\\\\\\\\\\folder1" it is also navigating to the same path, creates folder and not giving any error, this is a problem for me.
So in order to solve the above mentioned problem I try to do some validation before on the path and I tried with Path.GetFullPath() and other Path methods and I see:
Path.GetFullPath("C:\\\\folder1") no exception or error
Path.GetFullPath("C:\\\folder1") exception or error
somehow when the count of backslashes are in even number no exception is thrown but when the count is in odd number then exception is thrown.
How can I achieve this simple thing that when user enters path like:
C:\folder 1 valid path
C:\\\\\\folder1 invalid path
Please let me know if further details are required
Possible solution using FolderBrowserDialog - Users will not manually input the path but rather select/create it via FolderBrowserDialog.
The below code will return all the files in a folder but you can amend it to return whatever information you need.
private void Form1_Load(object sender, EventArgs e)
{
//
// This event handler was created by double-clicking the window in the designer.
// It runs on the program's startup routine.
//
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
}
Code found here
If you want to get a proper path from that, maybe you can use the following technique (in addition to what you already have, of course, this is only to remove the repeated backslashes)
Split the path using the '\' character
Remove the empty values (you can filter here the non valid
characters, etc)
Reconstruct the path string using join with the character '\' again
Something like:
pathString = "C:\\\\\\folder1";
splitString = pathString.Split('\\');
nonEmpty = splitString.Where(x => !string.IsNullOrWhiteSpace(x));
reconstructed = string.Join("\\", nonEmpty.ToArray());
Test code here: https://dotnetfiddle.net/qwVqv8
What about sanitizing the path?
char[] separator = new char[] { System.IO.Path.DirectorySeparatorChar };
string inputPath = "C:\\\\\\\folder1";
string[] chunks = inputPath.Split(separator, StringSplitOptions.RemoveEmptyEntries);
string validPath = String.Join(new string(separator), chunks);
So on my previous question (C# Only File NAMES in ListBox) I asked how to show only the file names.I got that to work. Then I encountered another problem: I could not load whats in the directory because there is no way to. A user told me
"
You either need to use a Dictionary datasource for your ListBox (with the key being the file name and the value being that path) See this answer for an idea of what I mean. Or you need to rebuild the path in your
IndexChange function (using Path.Combine() )
"
And me being me, I had no clue what he meant. So I came back for more help. I have not put any code as I don't know how to.
https://msdn.microsoft.com/it-it/library/fyy7a5kt(v=vs.110).aspx
string folder = #"C:/Aatrox";
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var fileName = (string)ListBox1.SelectedItem;
textEditorControl1.Text = File.ReadAllText(Paht.Combine(folder, fileName));
}
private void FlatButton3_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
string[] txtfiles = Directory.GetFiles(folder, "*.txt");
string[] luafiles = Directory.GetFiles(folder, "*.lua");
foreach (var item in txtfiles)
{
ListBox1.Items.Add(Path.GetFileName(item));
}
foreach (var item in luafiles)
{
ListBox1.Items.Add(Path.GetFileName(item));
}
}
If I understand correctly, you want a List of file names from a certain directory. You want to use Directory.EnumerateFiles to get each file in the directory. Path.Combine only combines a directories path, for modularity and use on other PC's mainly, such as Path.Combine(Environment.CurrentDirectory, "Hello").
I would like to check file exists in the same folder where is my program. If is do something. How i can fix that ?
private void button12_Click(object sender, EventArgs e)
{
if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "tres.xml"))
Upload("tres.xml");
}
The reason why your code doesn't work is that GetDirectoryName returns no \ at the end. That's even documented:
The string returned by this method consists of all characters in the
path up to but not including the last DirectorySeparatorChar or
AltDirectorySeparatorChar
Use Path.Combine to get the correct directory separator char:
string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "tres.xml");
if(File.Exists(path))
{
// ...
}
You can just simply use:
File.Exists("tres.xml");
This checks the current directory of your .exe
I am relatively well versed in C#, having worked in Unity (game engine) for a good 5 years now, but don't go near Windows much, just a heads up if it's something stupid I've missed.
In my WPF application, I am trying to look for a file in the root application location (\bin\Debug), but I keep receiving this error when I run it.
An exception of type 'System.IO.IOException' occurred in mscorlib.dll
but was not handled in user code Additional information: The directory
name is invalid.
Hopefully someone here can see what I've done wrong, and aid me in fixing this.
I am working on this as a side project for a friend who needed some help from someone who knows c#, but I've never gone near the Windows side of things before. (It's being run in the "public MainWindow()" initializer).
InitializeComponent();
string[] filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "settings.txt");
string fileContent = filePath.ToString();
string[] contentArray = fileContent.Split(':');
string contentColor;
string contentIntensity;
for (int i = 0; i < contentArray.Length; i++)
{
switch (i)
{
case 0:
{
if (contentArray[i].ToLower().Contains("color: "))
{
contentColor = contentArray[i].Replace("title: ", "");
}
if (contentArray[i].ToLower().Contains("intensity: "))
{
contentIntensity = contentArray[i].Replace("intensity: ", "");
}
break;
}
}
}
What were you thinking here?
string[] filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "settings.txt");
string fileContent = filePath.ToString();
It should be written as :
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.txt");
string fileContent = File.ReadAllText(filePath);
you could do something like this
var filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).
Where(name => name.Contains("settings")).ToList();
or
var filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).
Where(name => name.EndsWith(".txt")).ToList();
or return a string[]
var filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).
Where(name => name.EndsWith(".txt")).ToArray();
From MSDN
Directory.GetFiles :
Returns the names of files (including their paths) that match the
specified search pattern in the specified directory.
The method GetFiles accepts one or two parameters, a path or a path and a search pattern, the path has to be a directory !
IOException occurs when :
Path is a file name or there is a network error, and you are in the first case.
the method argument you are giving (AppDomain.CurrentDomain.BaseDirectory + "settings.txt") which is a file name not a directory, that's why you are having that exception.
So, instead of calling GetFiles on a File !! Just combine the two strings to get a file path by doing :
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.txt");
I've googled about this all over the Internet and still haven't found a solution. As an ultimate try, I hope someone can give me an exact answer.
I get that error when I try to copy a file from a directory to another in an File Explorer I'm trying to do on my own. It has a treeview control to browse for directories and a listview control to display the contents of the directory. This is how the code would look like, partially:
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
sourceDir = treeView1.SelectedNode.FullPath;
for (int i = 0; i < listView1.SelectedItems.Count; ++i)
{
ListViewItem l = listView1.SelectedItems[i];
toBeCopied[i] = l.Text; // string[] toBeCopied, the place where I save the file names I want to save
}
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
targetDir = treeView1.SelectedNode.FullPath;
try
{
for (int i = 0; i < toBeCopied.Length; ++i)
{
File.Copy(sourceDir + "\\" + toBeCopied[i], targetDir + "\\" + toBeCopied[i], true);
refreshToolStripMenuItem_Click(sender, e);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.TargetSite);
}
}
The place where I got the error is at File.Copy(sourceDir + "\\" + toBeCopied[i] ....
I've read that it could be something that has to do with the mapping of devices, but I don't really know what that is.
Can you take a look at the Path.Combine method on MSDN? This will help make sure all your entire path doesn't have extra \'s where they shouldn't be.
i.e. Path.Combine(sourceDir, toBeCopied[i])
If you are still getting an error, let me know what the value if the above.
Does the target path up to the file name exist? File.Copy() will not create any missing intermediate path, you would need to do this yourself. Use the debugger to see both the source and target paths you are creating and make sure the source exists and the target exists at least up to the parent of the target file.
You do not show where toBeCopied is created. It looks like you are probably running past the end of the values that are set in the click event, and trying to copy a bunch of files with empty names.
You should add this to the beginning of your click event
toBeCopied = new string[listView1.SelectedItems.Count];
Also (as others have noted) instead of
sourceDir + "\\" + toBeCopied[i]
you should use
Path.Combine(sourceDir, toBeCopied[i])
Assuming both sourceDir and targetDir exist (which you can and should check), you might be doubling up a trailing \. When building paths, you should use Path.Combine.
File.Copy(Path.Combine(sourceDir, toBeCopied[i]), Path.Combine(targetDir, toBeCopied[i]), true);
Borrowing from Henk's loop, but I'd add the file & directory checks, since it is the path not found errors that need checking/creating that the OP has the problem with.
for (int i = 0; i < toBeCopied.Length; ++i)
{
string sourceFile = Path.Combine(sourceDir, toBeCopied[i]);
if(File.Exists(sourceFile))
{
string targetFile = Path.Combine(targetDir, toBeCopied[i]);
if(!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
File.Copy(sourceFile, targetFile, true);
}
refreshToolStripMenuItem_Click(sender, e)
}