blah blah blah is here! blah blah » Close

up1down
link

I'm just trying to keep two string values (filename & path) separate and later trying to combine them......

string sourcepath = "V:\\Common\\gsvirdi\\";
string bakpath = "E:\\gsvirdi\\backup";
string filename = sourcepath + "LODs.xml";
string bakfilename = bakpath + "LODs.xml";

But this is not working out, why I'm doing this bcoz: I want to make a backup if the lods.xml in another drive so that incase the original file is removed due to periodic cleaning of the network drive... I can retireve the backup.

My prob is that I need filename & bakfilename to be public, and so both paths should also be publically accessible. What should I do?

last answered one year ago

4 answers

up1down
link

Well, if you want the paths to be publicly accessible, then you'll need to declare the strings as fields rather than as local variables. If the paths are to be hard-coded you could use constants, like this:

using System;

public static class Paths
{
private const string sourcepath = "V:\\Common\\gsvirdi\\";
private const string bakpath = "E:\\gsvirdi\\backup\\"; // Don't forget final '\\'
public const string filename = sourcepath + "LODs.xml";
public const string bakfilename = bakpath + "LODs.xml";
}

static class Program
{
static void Main()
{
Console.WriteLine(Paths.filename);
Console.WriteLine(Paths.bakfilename);
Console.ReadKey();
}
}

If they're not (or not entirely) to be hard-coded, then I'd use public static properties instead.

If you only want the paths to be accessible by other classes in the same program (not by external classes), then use 'internal' rather than 'public' throughout.

gsvirdi
412

The XML file contains names of "documents to be delivered", Boss does not want all users to know XML's Location on the Network drive. Contents of XML can't be password protected, I was not wishing to hard code the path bcoz changing the path later-on will need the exe file to be redistributed(Stupid Option). Please suggest me a better option for this situation.

vulpes
17279

If you're trying to hide the XML file so the users can't find it, then you could place it in something called IsolatedStorage. However, it's awkward to use (you can't use ordinary file IO to access it but need to use a special stream instead). A better idea would be to bury it in one of the network drive's special folders (see http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx) under a non-obvious name and then access it via the Environment.GetFolderPath() method. You won 't then need to hard code the folder path.

up0down
link

Comments section is not working again for me.....

Okie, I liked that GetFolderPath() suggestion, but sadly the Network Admin had limited the access of users. All users are given access to some particualr folders (projects) on Network (disk). So the only option for me as a developer is to use the common drive(folder) so that my application can access the source file from any computer on the Network. Ex: I have access of a folder \\IP address\Proj1 ..... only. So I can't access Spl folders on network.

Only thing my small brain can think of is that I use filename's extension as XML and Bakfilename's as something goofy. What do u say?

I'm having another Prob. I just envisaged a situation where two people (different computers) Edit the xml file at the same time!!!!

Can u plz suggest how can I lock the xml file so that two ppl should not edit it at the same time?

GOSH.... I created a mess instead of a utility???? Wow....

vulpes
17279

Yep, changing the file extension shouldn't be a problem to the various XML classes in the .NET framework. With regard to multiuser access, when you create a FileStream, it's possible (with some constructor overloads) to specify a FileShare argument which controls read and write access to the file by other processes (see http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx). The FileStream class also has a Lock() method which enables you to prevent other processes (even if they have Read/Write access) from accessing a part of the file until you call the Unlock() method. Of course, simultaneous reads of the file are not normally a problem but writes may be. Even if you use these approachs to control access, a good strategy in any multiuser environment is to only open a file for as long as you have to. Also you may need to review your try/catch clauses when doing file operations in case an exception is thrown because you don't currently have access to the file.

up0down
link

My apologies... I'm not able to implement FileStream thing in my code

up0down
link

Another option if the issue is that the boss does not want the users editing or seeing the data in the xml files you can encrypt the files. I needed to store Sensitive data in a file and got some help here to that which worked great!!

http://www.debugging.com/bug/22860

The other thing I did was to make sure that the name and extension of the file that was saved did not tip off what it contained.

Feedback