• 0

[C#] Multivariable Paths, Syntax Help.


Question

Ok, I'm a little new to C#, well new enough not to know this. I am trying to include a Multi-Variable Path in a StreamWrite. Here's an example of what I'm talking about:

				StreamWriter sw = new StreamWriter(@"C:\" + Variable1(As String) + @"\" + Variable2(As String) + ".AMNZ", true);
				sw.WriteLine(textBox1.Text);
				sw.WriteLine(sw.NewLine);
				sw.Close();

The "Variable(As String)" is a Variable I'm using as a string, it's not accualy like that in my code, I'm just saying it's not an int, double, or something else.

Everytime I run this I get "Illegal characters in path.".

I think this is because of the

@"\"

in the line. This is necesary because I'm am looking at a location that is defined by the user that has two depths of unknown location (but is defined by the user), which would be

Variable1(1st step) + @"\" + Variable2(2nd step)

.

So, I need to know how to add

@"\"

without getting this error, "Illegal characters in path.".

Thanks abunch,

Chris

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You could just write "\\" instead of @"\". \\ is the escape sequence for the backslash. I'm not sure that's going to fix your problem though; the invalid characters probably come from your variables.

Edited by Dr_Asik
Link to comment
Share on other sites

  • 0

I had tried this before and it didn't work. I jsut tried it again, and I recieved the same result. Nice thinking though. Anything else that we could use. I was think about maybe forming a Command.WriteLine since comand lines are sensitive to "\", are they? Just food for though. I'll keep trying, but if anyone else has some input that would be wonderfull!

Thanks,

Chris

Link to comment
Share on other sites

  • 0

StreamWriter sw = new StreamWriter(Path.Combine(path, fileName + ".AMNZ"));

You can also construct a FileInfo class with the path then use it for opening, creating, or appending.

FileInfo f = new FileInfo(Path.Combine(path, fileName +".AMNZ"));
StreamWriter sw = f.CreateText();
StreamReader sr = f.OpenText();

Link to comment
Share on other sites

  • 0

Ok, I found the problem. Here's the basics of the program (this will help explain the probelm).

So, when you start the program it prompts the user to login. On login it creates a hiden file with that user's encypted credentals, then opens another form so that the user can carry out the porgrams purpose. After that second form starts, it then reads the file and sets it's content as a string, and then deletes the hidden file with the encrypted user credentals. The problem lies with the reading and writing of that file. Aparently when it writes the file, it adds another line so that another line can be written, but this is not needed. When it reads the file and saves it as a variable it turns out like this:

User Credentals

(extra line) <-- PROBELM

So when I add another piece of text to it such as the "\\" or the ".AMNZ" it turns out like this:

User Credentals

\\ or .AMNZ

I need it to be like this

User Credentals(then nothing)

So that this is posible:

User Credentals \\ or .AMNZ

Is there any other way to pass that info without a temp file? Or just fix the extra line problem?

Link to comment
Share on other sites

  • 0

You shouldn't need to do that with a temporary file. You can just create a structure with the username/password and store the password as a hash. Also, have you looked into using the built-in security in .NET and just using the Windows authentication?

Link to comment
Share on other sites

  • 0

Like I said, I'm new, and this is the way I stored info for my batch files to recall variables in a later period in time.

I don't want the program to authenticate using their current Windows username and password. I need them to have their own to prevent anyone that can login using their account from having access to it... Also, it is a way of dividing infomation that they have stored on their computer.(Say a family is using this program, and one person wants to use it and the other also wants to use it, if I do it the way you are thinking of it is only proible in a private manner for one of the people to use it. If I do it where they can create seperate accounts and login seperatly so they can have their own private info stored, both have an equal amount of privacy while using this program...)

So what exactly is "hash" or a "hash"? Is that somthing that could send a variable from one form to the next(bacause that my basic goal)?

Link to comment
Share on other sites

  • 0

A hash is just a checksum of something else. If you use the MD5 crypto class you can generate a "hash value" from an entered password that is always the same as long as the password is identical. That way you can store the password without actually, er, storing the password. :) (You just hash the login password and compare it to the original hash from when the account info was entered.)

What I was wondering is if you are storing the login credentials for later use, or simply to pass them to another part of the same program? If it's the latter, it would be easier just to use a class or structure to hold the data and not create a temporary file.

Here is a very simple example I tossed together to illustrate what I'm talking about:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			UserCredentials user = UserCredentials.Create("foo", "bar");
			Console.WriteLine("Creating credentials. User='{0}'", user.Username);
			Console.WriteLine("Password 'banana' is {0}", user.Authenticate("banana"));
			Console.WriteLine("Password 'foo' is {0}", user.Authenticate("bar"));
			Console.WriteLine();
			Console.Write("Press RETURN to exit.");
			Console.ReadLine();
		}

		class UserCredentials
		{
			public string Username { get; set; }
			internal byte[] Password { get; set; }

			public static UserCredentials Create(string username, string password)
			{
				UserCredentials userData = new UserCredentials();

				userData.Username = username;

				MD5 md5 = MD5.Create();
				userData.Password = md5.ComputeHash(Encoding.Unicode.GetBytes(password));

				return userData;
			}

			public bool Authenticate(string password)
			{
				MD5 md5 = MD5.Create();
				byte[] hash = md5.ComputeHash(Encoding.Unicode.GetBytes(password));

				if (hash.Length != this.Password.Length)
					return false;

				for (int c = 0; c &lt; hash.Length; c++)
				{
					if (hash[c] != this.Password[c])
						return false;
				}

				return true;
			}
		}
	}
}

You can add a function or two to the UserCredentials class to save and load it.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.